Haplar
Haplar

Reputation: 21

Java EE Authentication Error Handling

We are currently trying to implement a web application which is using the Java EE authentication mechanism with a FORM-based login, inside the Websphere 6.1 web container. If the authentication is successful, we have everything working; the LDAP membership groups are being retrieved, the group to role mapping is being performed, and the roles are being returned to and correctly interpreted by the web application.

If the authentication is not successful, the form-login-error page is being returned. However, this is a static page that simply says something like "there was an error". How do we trap the specific error that prevented the successful login (username/password incorrect, LDAP repository unavailable, account locked, password expired, etc.)? It seems like there should be some easy way to do this, as you would want to treat some "security" exceptions differently than others.

Upvotes: 2

Views: 6584

Answers (4)

jim
jim

Reputation: 1552

I use Struts so it will do forwarding for you. If you don't have a framework (why not?) you'll will have to do it manually.

The Java EE spec covers the j_security_check servlet.

The login page POSTs j_username and j_password to the j_security_check servlet. Your app will be configured to error to an unauthorized page (see web.xml) but will (initially) call a servlet. 401 or 403 will go to a forbidden page (again web.xml)

Inside that servlet (which extends the HttpServlet) - you will check for all that good stuff.

public final void doGet(javax.servlet.http.HttpServletRequest request,
    javax.servlet.http.HttpServletResponse response)
    throws javax.servlet.ServletException, java.io.IOException
{
    // initialize the app
    AppInit initializer = new AppInit();

    // get the logger
    log = new Log4jWrapper(this.getClass());

    // initialize the application session
    HttpSession sess = request.getSession(true);
    sess.setAttribute(CommonConstants.SESSION_CURR_USER_ID, request.getRemoteUser());

    // initialize the JSP to forward to based on the user role
    String fwdJSP = "SetupMainPage.jsp";
    if (request.isUserInRole(CommonConstants.ROLE_MANAGER)) {
        log.debug("User is a Manager");
    }
    //else other role checks - (these are users in groups in the LDAP)
    // initialize the application session and set a variable to indicate that
    // we are coming from a first time login (not a timeout login)
    sess.setAttribute(CommonConstants.SESSION_COMING_FROM_INITIAL_LOGIN,"TRUE");
    disp = getServletContext().getRequestDispatcher("SetupMainPage.jsp");
    disp.forward(request, response);
}
//else failure

Unknown user

[11/22/08 8:54:47:993 EST] 7f6ac69c FormLoginServ E SECJ0118E: Authentication error during authentication for user s

right user - wrong password, but the request.getRemoteUser() will have a value

[11/22/08 8:56:45:082 EST] 7f51469c FormLoginServ E SECJ0118E: Authentication error during authentication for user jbsymolo

Unfortunately - i don't have any examples of someone locked out but I going to assume that the main security directory (LDAP) you will have an entry for the user for that.

This is from someone else (so I can't take credit)

I think this page describes how to do what you want to do.

Specifically how to retrieve the authentication exception from an arbitrary underlying authentication source (looks like Websphere calls them user registries).

Throwable t = com.ibm.websphere.security.auth.WSSubject.getRootLoginException();
if (t != null)
t = determineCause(t);

Where determineCause() is defined on the same page. This way, even if your server is configured to authenticate against a John Deer tractor, you will have access to the "OutOfGasLoginException" if there is one. The above code can go into the Servlet, Servlet Filter, or JSP that is redirect to by the container (as described above by jsymolon). It simply examines the exceptions and then places a corresponding friendly error message on the resulting page.

Upvotes: 1

Yves Martin
Yves Martin

Reputation: 10381

The JavaEE specification does not provide a standard mean to get an authentication feedback like error codes.

According to the following IBM Redpaper about z/OS security integration in gray note on page 57: IBM specific extension is available so that the error page JSP can report a specific message (like password expired) based on an error status code.

According to the WebSphere InfoCenter the FormLoginWeb sample from the TechSamp package in your WebSphere installation (samples/src/TechSamp/FormLoginWeb) is supposed to demonstrate such IBM specific extension but... The only thing interesting is the LoginFilter that intercepts calls on /j_security_check and is able to do pre-login validation and post-login action as explained in details in that paper.

With such a mechanism it is possible to get login exception from JAAS Subject and set an login error code in HttpSession so that the error page can generate a specific message.

Upvotes: 0

Philip Tinney
Philip Tinney

Reputation: 2016

Check this article Securing J2EE Applications with a Servlet Filter. I believe it covers your requirement to be able to pass the reason for the authentication error.

Upvotes: 0

Olaf Kock
Olaf Kock

Reputation: 48087

This is ancient knowledge - I believe to have done such a thing with tomcat. There was, as far as I can remember, no standard way, as the implementation was completely decoupled from the request and frontend web stuff, so that it was difficult to establish any means of communication between the authenticating component and the frontend (e.g. error page).

We ended up with a tomcat specific way, relying heavily on the current implementation. I'm no longer with that company, so I can't tell about the current state of the code or the solution we chose back then. I believe you'll also have to have some Websphere specific solution - be it the use of thread local variables, keying messages with the username that attempted to log in, somehow getting hold of the session identifier or similar.

Upvotes: 0

Related Questions