Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple java servlet authentication methods

Tags:

java

Is it possible to have multiple authentication methods for a java servlet? For example, have form based authentication in addition to open id based authentication so users can choose how they log in.

like image 342
knpwrs Avatar asked Jun 18 '10 02:06

knpwrs


2 Answers

Yes.

However, I would suggest doing this using servlet filters instead of on the servlet itself.

http://brendangraetz.wordpress.com/2010/06/17/use-servlet-filters-for-user-authentication/

Follow the steps in that post, and override the isAuth() method such that it performs the authentication in however many modes you wish. In (very rough, untested) code:

@Override protected boolean isAuth()
{
    String authMode = (String)(getSession(true).getAttribute("authMode"));
    if (authMode == null) { return false; }
    if (authMode.equals("open id") {
        //do open id authentication steps here
        //return true if authentication passes
    }
    else if (authMode.equals("some other authentication") {
        //do some other authentication steps here
        //return true if authentication passes
    }
    ...
    return false;    
}

I am assuming of course that you already know how to implement the authentication steps in each mode individually.

The "trick" is to store a value in the HTTP session, immediately after the user performs the log in authentication, in the HTTP session. Based on this value, the filter will know what it should check or query whatever you specify before loading the servlet.

like image 50
bguiz Avatar answered Oct 06 '22 05:10

bguiz


Another way of performing multiple authentication is with JAAS, the Java Authentication and Authorization service. Using JAAS, you can stack various authentication modules on top of each other, and you can configure which authentication module to run and which not to. This is called PAM (pluggable authentication module). Search for "J2SE JAAS" on Google or take a look at http://server.pramati.com/docstore/1270002/index.htm. These should help you to get started if you decide to go this route.

like image 29
WindsurferOak Avatar answered Oct 06 '22 05:10

WindsurferOak