Develop with the AuthDataProvider API to obtain authentication data
We can use the AuthDataProvider Application Program Interface (API) to obtain the authentication data from the application.
Your application can use the AuthDataProvider API to obtain an AuthData object containing the user name and password configured for an authData element.
- Add the passwordUtilities-1.0 feature in the server.xml
file. For example:
<featureManager> <feature>passwordUtilities-1.0</feature> </featureManager>
- Configure an authData element in server.xml. For
example:
<authData id="myAuthData" user="myUser" password="myPassword"/> <!-- password can also be encoded -->
Encode the password within the configuration. We can get the encoded value using the securityUtility encode command.
- Use the AuthDataProvider API from the application servlet or enterprise bean
as follows, replacing the authData alias with the one we need. For example:
AuthData authData = AuthDataProvider.getAuthData("myAuthData"); // Replace value with your alias.
Note that the error handling is not shown for simplicity. A javax.security.auth.login.LoginException is encountered if the authentication alias requested does not exist or is malformed.
- Obtain the user name and password from the AuthData object. For example:
String userName = authData.getUserName(); char[] password = authData.getPassword(); // Do something with the userName and password.