開發用於資料庫鑑別的 JAAS 自訂登入模組

您可以開發一個「Java™ 鑑別和授權服務 (JAAS)」自訂登入模組,用來新增使用者名稱和密碼,以接受資料庫的鑑別。

關於這項作業

您可以開發一個在建立需要鑑別的資料庫連線時,所能呼叫的 JAAS 自訂登入模組。 這個 JAAS 自訂登入模組負責建立一份含有使用者名稱、密碼及受管理 Connection Factory 的密碼認證。 這個登入模組必須將密碼認證新增至設定用來接受資料庫鑑別的主體 (subject) 專用認證中。

程序

  1. 建立一個實作 javax.security.auth.spi.LoginModule 介面的類別。
  2. 將必要的欄位儲存在起始設定方法中。例如:
    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) {
      this.callbackHandler = callbackHandler;
      this.subject = subject;
      this.sharedState = (Map<String, Object>) sharedState;
      this.options = options;
    }
  3. 在 login 方法中處理 WSManagedConnectionFactoryCallbackWSMappingPropertiesCallback 回呼。 例如:
    /** {@inheritDoc} */
    @Override
    public boolean login() throws LoginException {
      ...
      Callback callbacks[] = new Callback[2];
      callbacks[0] = new WSManagedConnectionFactoryCallback("Target ManagedConnectionFactory: ");
      callbacks[1] = new WSMappingPropertiesCallback("Mapping Properties (HashMap): ");
      callbackHandler.handle(callbacks);
  4. 在 loging 方法中,取得受管理 Connection Factory 及各個內容。例如:
    // The method getManagedConnectionFactory must be used as shown for compatibility with WebSphere traditional
    ManagedConnectionFactory managedConnectionFactory = ((WSManagedConnectionFactoryCallback) callbacks[0]).getManagedConnectionFacotry();
    Map properties = ((WSMappingPropertiesCallback) callbacks[1]).getProperties();
  5. 根據鑑別資料別名或某些其他準則來取得使用者名稱和密碼。例如:
    String alias = (String) properties.get(com.ibm.wsspi.security.auth.callback.Constants.MAPPING_ALIAS);
    String user = getUser(alias); // Implementation specific
    char[] password = getPassword(alias); // Implementation specific
  6. 利用使用者名稱和密碼建立一個 javax.resources.spi.PasswordCredential 物件,然後設定受管理 Connection Factory。例如:
    javax.resource.spi.security.PasswordCredential passwordCredential = new PasswordCredential(user, password);
    passwordCredential.setManagedConnectionFactory(managedConnectionFactory);
  7. 在 commit 方法中,將密碼認證新增至主體 (subject) 中。例如:
    /** {@inheritDoc} */
    @Override
    public boolean commit() throws LoginException {
      // Verify that the login was successful before adding the PasswordCredential to the subject.
      subject.getPrivateCredentials().add(passwordCredential);
      return true;
    }

指示主題類型的圖示 作業主題



「時間戳記」圖示 前次更新: 2016 年 11 月 30 日
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twlp_dev_jaas_custom_login_module
檔名:twlp_dev_jaas_custom_login_module.html