I have a JSP which basically login using JAAS (the simple login module).  After login I can obtain the "Subject" by calling loginContext.getSubject() but I could not get the "Subject" from the request.getUserPrincipal().  How do I do it in such a way that I can programmatically log someone in and then able to use request.getUserPrincipal() and request.isUserInRole("") calls.
I am using JBoss-2.4.3-Tomcat-4.0.  Any help is appreciated.
 
Kar
 
PS.  Here is the JSP
 
<%@ page language='java' import='java.io.*,javax.security.auth.*,javax.security.auth.login.*,javax.security.auth.callback.*' %>
<%!
static class MyCallbackHandler implements javax.security.auth.callback.CallbackHandler {
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
     for (int i = 0; i < callbacks.length; i++) {
         if (callbacks[i] instanceof TextOutputCallback) {
          TextOutputCallback toc = (TextOutputCallback)callbacks[i];
          switch (toc.getMessageType()) {
              case TextOutputCallback.INFORMATION:
                   System.out.println(toc.getMessage());
                   break;
               case TextOutputCallback.ERROR:
                   System.out.println("ERROR: " + toc.getMessage());
                   break;
               case TextOutputCallback.WARNING:
                   System.out.println("WARNING: " + toc.getMessage());
                   break;
               default:
                   throw new IOException("Unsupported message type: " +
          toc.getMessageType());
           }
 
          } else if (callbacks[i] instanceof NameCallback) {
           NameCallback nc = (NameCallback)callbacks[i];
           nc.setName("testPassword");
          } else if (callbacks[i] instanceof PasswordCallback) {
           PasswordCallback pc = (PasswordCallback)callbacks[i];
           pc.setPassword(new char[] {'t','e','s','t','P','a','s','s','w','o','r','d' });
          } else {
           throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
          }
     }
    }
}
%>
<%
 LoginContext lc = null;
 try {
     lc = new LoginContext("simple", new MyCallbackHandler());
 } catch (LoginException ex) {
     ex.printStackTrace();
 }
 
    try {
     lc.login();
out.println(lc.getSubject() + "<br>");
    } catch (AccountExpiredException aee) {
  System.out.println("Your account has expired.  " + "Please notify your administrator.");
    } catch (CredentialExpiredException cee) {
  System.out.println("Your credentials have expired.");
    } catch (FailedLoginException fle) {
  System.out.println("Authentication Failed");
    } catch (Exception e) {
  System.out.println("Unexpected Exception - unable to continue");
  e.printStackTrace();
    }
    out.println(request.getUserPrincipal());
%>

Reply via email to