使用堆栈化 JAAS 登录模块来生成动态 LTPA 令牌

使用 WSS SPI 时,可以动态地将用户名和密码传递到 LTPA 令牌生成者 LTPAGenerateLoginModule。但是,如果必须使用策略集和绑定,那么无法在标准配置中执行此操作,因为回调处理程序以及回调处理程序中的用户名和密码都是固定值。如果使用了定制 JAAS 登录模块,那么可以使用策略集和绑定来创建动态 Kerberos 令牌。

开始之前

如果创建定制 JAAS 登录模块,或将 UsernameToken 添加至客户机的请求上下文,那么可以定制 LTPA 令牌生成者在创建 LTPA 令牌时所使用的用户名和密码。您也可以通过直接设置 LTPA 令牌生成者所使用的令牌字节来定制令牌。如果应用程序开发者想要编写代码以从入站请求的 HTTP 头中获取 LTPA 令牌,并将 LTPA 令牌放入出站 SOAP 消息的安全性头中,那么设置令牌字节很有用。

关于此任务

GenericSecurityTokenFactory 提供的 SPI 可用来创建令牌,而 LTPAGenerateLoginModule 可以使用该令牌来定制此模块所生成的 LTPA 令牌。这些 SPI 使您可使用动态用户名和密码,或者直接设置令牌字节,而回调处理程序并未提供这些功能。

以下过程显示如何使用堆栈化 JAAS 登录模块方法来定制用户名和密码或令牌字节。在完成此过程之后,您也可以将新创建的令牌放在客户机的请求上下文上。有关如何将令牌放在客户机的请求上下文上的更多信息,请参阅 com.ibm.wsspi.wssecurity.core.Constants Javadoc 中的下列常量:

  • com.ibm.wsspi.wssecurity.token.tokenHolder
  • com.ibm.wsspi.wssecurity.token.enableCaptureTokenContext

过程

  1. 创建动态令牌。

    完成下列其中一个子步骤。

    1. 要定制用户名和密码,请创建简单 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());
    2. 要定制令牌字节,请创建简单 BinarySecurityToken
      import com.ibm.websphere.wssecurity.wssapi.token.GenericSecurityTokenFactory;
      import com.ibm.websphere.wssecurity.wssapi.token.BinarySecurityToken;
      import com.ibm.websphere.wssecurity.wssapi.WSSUtilFactory;
      ...
      GenericSecurityTokenFactory gstFactory = GenericSecurityTokenFactory.getInstance();
      WSSUtilFactory utilFactory = WSSUtilFactory.getInstance();
      
      //callbackHandler is obtained from the initialize method in the Login Module
      Map reqHeaders = utilFactory.getHTTPRequestHeaders(callbackHandler);
      
      //getLtpaHeaderBytes is a method written by you to find the header that 
      //has the LTPA token in it in the header list, then return the encoded 
      //bytes out of the header.
      String encodedBytes = getLtpaHeaderBytes(reqHeaders); 
      
      byte [] decodedBytes = utilFactory.decode(encodedBytes);
      
      BinarySecurityToken bst = factory.getSimpleBinarySecurityToken(com.ibm.websphere.wssecurity.wssapi.token.LTPAv2Token, decodedBytes);
  2. 创建 JAAS 登录模块。
    package test.tokens;
    
    import com.ibm.websphere.wssecurity.wssapi.token.GenericSecurityTokenFactory;
    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.BinarySecurityToken;
    
    
    public class MyUntCustomLoginModule implements LoginModule {
      //For the sake of readability, this login module does not
      //protect against all NPE's
    
      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 {
    
        GenericSecurityTokenFactory factory = null;
        try {
          factory = GenericSecurityTokenFactory.getInstance();
        } catch (Exception e) {
          throw new LoginException(e.toString());
        }
    
        //code to create the BinarySecurityToken can be used
        //here instead of the simple UsernameToken
        UsernameToken unt = factory.getSimpleUsernameToken("myUsername", "myPassword".toCharArray());
    
        factory.putGeneratorTokenToSharedState(this._sharedState, unt);
    
        return true;
      }
      //implement the rest of the methods required by the
      //LoginModule interface
    }
  3. 创建新的 JAAS 登录配置。
    1. 在管理控制台中,选择安全性 -> 全局安全性
    2. 在认证下,选择 Java 认证和授权服务
    3. 选择系统登录
    4. 首先创建具有定制模块的生成者。
      1. 单击新建,然后指定别名 = test.generate.ltpa
      2. 单击新建,然后指定模块类名 = test.tokens.MyUntCustomLoginModule
      3. 选择使用登录模块代理
      4. 单击确定
      5. 单击新建,然后选择 com.ibm.ws.wssecurity.wssapi.token.impl.LTPAGenerateLoginModule
      6. 单击确定
      7. 单击 JAAS - 系统登录
  4. 配置 LTPA 令牌生成者以使用新的 JAAS 配置。
    1. 打开要更改的绑定配置。

      在管理控制台中,选择 WS-Security > 认证和保护

    2. 在“认证令牌”下,选择要更改的出站 LTPA 令牌。
    3. 选择 JAAS 登录 = test.generate.ltpa
    4. 单击保存
    5. 重新启动应用程序服务器以应用 JAAS 配置更改。
    6. 测试您的服务。

指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twbs_create_dynamicltpatoken
文件名:twbs_create_dynamicltpatoken.html