Developing a programmatic login for obtaining authentication data

You can use the Java Authentication and Authorization Service (JAAS) login framework to obtain the authentication data from your application.

About this task

Your application can perform a JAAS programmatic login using the DefaultPrincipalMapping JAAS context entry name to obtain a Subject object with a instance in the private credentials set that contains the user name and password configured for an authData element.


  1. Add the appSecurity-2.0 and passwordUtilities-1.0 features in the server.xml file. For example:
  2. Configure an authData element in the server.xml file. For example:
    <authData id="myAuthData" user="myUser" password="myPassword"/> <!-- password can also be encoded -->
    Encode the password within the configuration. You can get the encoded value by using the securityUtility encode command.
  3. Perform a programmatic login with the DefaultPrincipalMapping JAAS login context entry name from your application servlet or enterprise bean, replacing the mapping alias with the one you need. For example:
    HashMap map = new HashMap();
    map.put(, "myAuthData"); // Replace value with your alias.
    CallbackHandler callbackHandler = new, null);
    LoginContext loginContext = new LoginContext("DefaultPrincipalMapping", callbackHandler);
    Subject subject = loginContext.getSubject();
    Set<> creds = subject.getPrivateCredentials(;
    PasswordCredential passwordCredential = creds.iterator().next();
    Note: The error handling is not shown for simplicity. A is returned if the authentication alias requested does not exist or is malformed.
  4. Obtain the user name and password from the PasswordCredential. For example:
    String userName = passwordCredential.getUserName();
    char[] password = passwordCredential.getPassword();
    // Do something with the userName and password.
  5. If Java 2 Security is enabled, then the application must be granted the For example, grant the permission in the application's META-INF/permissions.xml file to access the PasswordCredential object:
    <?xml version="1.0" encoding="UTF-8"?>
    <permissions xmlns="" xmlns:xsi="" xsi:schemaLocation="" version="7">
        <name> * "*"</name>
      <!-- Other permissions -->

    For more information about , see Java 2 Security.