使用堆栈化 JAAS 登录模块来生成动态 UsernameToken
可以使用 GenericSecurityTokenFactory API 来创建完全填充或简单的 UsernameToken 安全性令牌以用于 WS-Security 运行时。这些安全性令牌可以用于(但不限于)WSSAPI 和 JAAS 登录模块或 UNTGenerateLoginModule。
关于此任务
GenericSecurityTokenFactory 提供若干 API,您可以将其用于创建 UsernameToken,其可以随 GenericIssuedTokenGenerateLoginModule 一起发出。
有两种类型的 UsernameToken:
- 完整 UsernameToken
- 完整 UsernameToken 包含 XML,并且可以与 GenericSecurityTokenFactory 登录模块一起发出。
- 简单 UsernameToken
- 简单 UsernameToken 仅包含用户名和密码;它不包含 XML。简单 UsernameToken 用来设置 UNTGenerateLoginModule、LTPAGenerateLoginModule 和 KRBGenerateLoginModule 可以使用的动态用户名和密码。
使用 GenericSecurityTokenFactory API 创建完整 UsernameToken 时,该令牌是可以由 WS-Security 运行时发出的安全性令牌的完整形式。确定要创建的令牌类型,然后发出类似下列其中一个步骤中所指定命令的命令以创建令牌。在创建令牌之后,无法修改令牌中的用户名和密码。
如果使用 GenericSecurityTokenFactory API 来创建简单 UsernameToken,那么该令牌仅包含用户名和(可选)密码。因为简单 UsernameToken 不包含 XML,所以不能与 GenericIssuedTokenGenerateLoginModule 一起发出。
过程
- 创建 UsernameToken。 可创建下列 UsernameToken 类型的其中一种:
- 带有用户名和密码的完整 UsernameToken
- 带有用户名和时间戳记(但没有密码)的完整 UsernameToken
- 带有用户名和密码的简单 UsernameToken
- 创建带有用户名和密码的完整 UsernameToken。
import com.ibm.websphere.wssecurity.wssapi.token.GenericSecurityTokenFactory; import com.ibm.websphere.wssecurity.wssapi.token.UsernameToken; ... GenericSecurityTokenFactory gstFactory = GenericSecurityTokenFactory.getInstance(); UsernameToken myUnt = gstFactory.getFullUsernameToken("myUsername", "myPassword".toCharArray());
- 创建带有用户名和时间戳记(但没有密码)的完整 UsernameToken。
import com.ibm.websphere.wssecurity.wssapi.token.GenericSecurityTokenFactory; import com.ibm.websphere.wssecurity.wssapi.token.UsernameToken; ... GenericSecurityTokenFactory gstFactory = GenericSecurityTokenFactory.getInstance(); UsernameToken myUnt = gstFactory.getFullUsernameToken("myUsername", null, true);
- 创建带有用户名和密码的简单 UsernameToken。
import com.ibm.websphere.wssecurity.wssapi.token.GenericSecurityTokenFactory; import com.ibm.websphere.wssecurity.wssapi.token.UsernameToken; ... GenericSecurityTokenFactory gstFactory = GenericSecurityTokenFactory.getInstance(); UsernameToken myUnt = gstFactory.getSimpleUsernameToken("myUsername", "myPassword".toCharArray());
- 创建 JAAS 登录模块。 如果在上一步中创建了完整 UsernameToken,那么必须创建 JAAS 登录模块,该 JAAS 登录模块堆叠在 GenericIssuedTokenGenerateLoginModule
上以发出完整 UsernameToken。如果在上一步中创建了简单 UsernameToken,那么必须创建 JAAS 登录模块,该 JAAS 登录模块可堆叠在 UNTGenerateLoginModule
上以发出动态 UsernameToken。
- 创建 JAAS 登录模块,该模块可以堆叠在 GenericIssuedTokenGenerateLoginModule 之上来发出完整 UsernameToken。如果使用的是 V8.5.0.2 或更高版本,那么以下示例适用:
package test.tokens; import java.util.HashMap; import java.util.Map; import javax.security.auth.Subject; import javax.security.auth.callback.CallbackHandler; import javax.security.auth.login.LoginException; import javax.security.auth.spi.LoginModule; import com.ibm.websphere.wssecurity.wssapi.token.UsernameToken; import com.ibm.websphere.wssecurity.wssapi.token.GenericSecurityTokenFactory; import com.ibm.websphere.wssecurity.wssapi.WSSUtilFactory; import com.ibm.wsspi.wssecurity.core.config.CallbackHandlerConfig; import java.util.ArrayList; public class MyFullUntGenerator implements LoginModule { private Map _sharedState; private Map _options; private CallbackHandler _handler; public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) { this._handler = callbackHandler; this._sharedState = sharedState; this._options = options; } public boolean login() throws LoginException { //For the sake of readability, this login module does not //protect against all NPE's GenericSecurityTokenFactory factory = null; try { factory = GenericSecurityTokenFactory.getInstance(); } catch (Exception e) { throw new LoginException(e.toString()); } if (factory == null) { throw new LoginException("GenericSecurityTokenFactory.getInstance() returned null"); } //The userid and password can be obtained however you want // (Optional) Obtain the username and password configured in the callback handler Map wssContext = getWSSContext(_handler); CallbackHandlerConfig chc = (CallbackHandlerConfig) wssContext.get(CallbackHandlerConfig.CONFIG_KEY); String username = chc.getUserId(); char[] password = chc.getUserPassword(); UsernameToken unt = factory.getFullUsernameToken("myUsername", "myPassword".toCharArray()); //Put the token in a list on the shared state where it will be available to be used by //stacked login modules factory.putGeneratorTokenToSharedState(_sharedState, unt); return true; } //implement the rest of the methods required by the //LoginModule interface }
- 创建 JAAS 登录模块,该模块可以堆叠在 UNTGenerateLoginModule 之上来发出动态 UsernameToken
package test.tokens; import java.util.HashMap; import java.util.Map; import javax.security.auth.Subject; import javax.security.auth.callback.CallbackHandler; import javax.security.auth.login.LoginException; import javax.security.auth.spi.LoginModule; import com.ibm.websphere.wssecurity.wssapi.token.UsernameToken; import com.ibm.websphere.wssecurity.wssapi.token.GenericSecurityTokenFactory; import com.ibm.websphere.wssecurity.wssapi.WSSUtilFactory; import com.ibm.wsspi.wssecurity.core.config.CallbackHandlerConfig; import java.util.ArrayList; public class MySimpleUntGenerator implements LoginModule { private Map _sharedState; private Map _options; private CallbackHandler _handler; public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) { this._handler = callbackHandler; this._sharedState = sharedState; this._options = options; } public boolean login() throws LoginException { //For the sake of readability, this login module does not //protect against all NPE's GenericSecurityTokenFactory factory = null; try { factory = GenericSecurityTokenFactory.getInstance(); } catch (Exception e) { throw new LoginException(e.toString()); } if (factory == null) { throw new LoginException("GenericSecurityTokenFactory.getInstance() returned null"); } //The userid and password can be obtained however you want // (Optional) Obtain the username and password configured in the callback handler Map wssContext = getWSSContext(_handler); CallbackHandlerConfig chc = (CallbackHandlerConfig) wssContext.get(CallbackHandlerConfig.CONFIG_KEY); String username = chc.getUserId(); char[] password = chc.getUserPassword(); UsernameToken unt = factory.getSimpleUsernameToken("myUsername", "myPassword".toCharArray()); //Put the token in a list on the shared state where it will be available to be used by //stacked login modules factory.putGeneratorTokenToSharedState(_sharedState, unt); return true; } //implement the rest of the methods required by the //LoginModule interface }
- 创建 JAAS 登录模块,该模块可以堆叠在 GenericIssuedTokenGenerateLoginModule 之上来发出完整 UsernameToken。
- 创建 JAAS 登录配置。
- 请记下您在上一步中创建的定制登录模块的完整类名。 例如,test.tokens.MyFullUntGenerator。
- 请记下您要堆叠在其上的登录模块的完整类名。 例如,com.ibm.ws.wssecurity.wssapi.token.impl.UNTGenerateLoginModule 或 com.ibm.ws.wssecurity.wssapi.token.impl.GenericIssuedTokenGenerateLoginModule。
- 在管理控制台中,转至安全性 > 全局安全性。
- 在“认证”下,转至 Java 认证和授权服务 > 系统登录。
- 单击新建,然后在“别名”下输入 test.generate.unt。
- 在“JAAS 登录模块”下,单击新建,然后在“模块类名”下输入定制登录模块的名称。选择使用登录模块代理,然后单击确定。
- 单击新建,然后在“模块类名”下输入要堆叠在其上的登录模块的名称。单击确定。
- 配置 UsernameToken 令牌生成者以使用新的 JAAS 登录配置。
- 在管理控制台中,打开要更改的绑定配置。
- 选择 WS-Security > 认证和保护。
- 在“认证令牌”下,选择要更改的出站 UsernameToken。
- 在“JAAS 登录”下,选择 test.generate.unt。
- 可选: 如果使用 GenericIssuedTokenGenerateLoginModule,请添加 passThroughToken=true 定制属性。
- 单击回调处理程序。
- 添加 passThroughToken=true 定制属性。
- 单击确定。
- 单击保存。
- 重新启动应用程序服务器以应用 JAAS 配置更改。
- 测试服务。


http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twbs_createusertokens
文件名:twbs_createusertokens.html