在应用程序中,可以使用 AuthDataProvider 应用程序编程接口 (API) 获取认证数据。
关于此任务
应用程序可使用 AuthDataProvider API 来获取 AuthData 对象,该对象包含针对 authData 元素配置的用户名和密码。
过程
- 在 server.xml 文件中添加 passwordUtilities-1.0 功能部件。例如:
<featureManager>
<feature>passwordUtilities-1.0</feature>
</featureManager>
- 在 server.xml 文件中配置 authData 元素。例如:
<authData id="myAuthData" user="myUser" password="myPassword"/> <!-- password can also be encoded -->
对配置中的密码进行编码。可以使用 securityUtility encode 命令来获取编码值。
- 从应用程序 Servlet 或企业 Bean 中使用 AuthDataProvider API,如下所示(将 authData 别名替换为所需的别名)。例如:
AuthData authData = AuthDataProvider.getAuthData("myAuthData"); // Replace value with your alias.
注: 为简单起见,不显示错误处理。如果所请求的认证别名不存在或格式错误,那么将遇到 javax.security.auth.login.LoginException。
- 从 AuthData 对象获取用户名和密码。例如:
String userName = authData.getUserName();
char[] password = authData.getPassword();
// Do something with the userName and password.