Discussions

General J2EE: getting credentials of logged in user

  1. getting credentials of logged in user (4 messages)

    Hi all

    I have a java application that accesses EJBs on a JBoss application server. My application requires the user to log in, providing username and password. After successful authentication, the user can access the application.

    I would now like to be able to extract the users credentials (username) while he is logged in and workin in the application. How can this be done? how can i extract credential from a current session?

    I currently use the following code (client side):
    -----------------------
    public static String getUserSubject(LoginContext lc) throws Exception
    {
    String subject = "";

    try
    {
    Subject sub = lc.getSubject();
    Set principals = sub.getPrivateCredentials();
    Object[] objCredentials = principals.toArray();
    String[] sPrincipals = null;
    for(int i=0; i< objCredentials.length; i++)
    {
    sPrincipals = (String)objCredentials;
    System.out.println("Credentials (lc): " +sPrincipals);
    }
    }
    catch (Exception ex)
    {
    logger.error("Problem getting login context information.");
    System.out.println("ERROR: "+ex);
    }
    return subject;
    }
    ---------------------------------------

    But the above code returns only 'null'. What's wrong here?

    Cheers,
    M.
  2. u set
    <code>String subject = "";</code>
    at the very beginning.

    but then u never do anything else with the variable again.

    are u expecting it to return the emptry string "" instead of null or what?
  3. Hi there

    Right you are. But that I am not currently relying on the subject string. I get nothing back if I, for example, use:

    Subject sub = lc.getSubject();
    Set principals = sub.getPrivateCredentials();
    System.out.println("Principals: "+principals.size());

    principals.size() returns 0, so there seems to be nothing to get...

    Any ideas?

    Michael
  4. No worries! I have been able to resolve the issue in the mean time.

    Cheers,
    Michael
  5. can you write the solution, please!! I've the same problem and I looking for a solution.


    thanks