開發取得鑑別資料的程式化登入

您可以利用「Java 鑑別和授權服務 (JAAS)」登入架構,從應用程式取得鑑別資料。

關於這項作業

您的應用程式可以利用 DefaultPrincipalMapping JAAS 環境定義項目名稱來執行 JAAS 程式化登入,以取得在專用的認證集中有 javax.resource.spi.security.PasswordCredential 實例包含 authData 元素所配置之使用者名稱和密碼的 Subject 物件。

程序

  1. server.xml 檔中新增 appSecurity-2.0passwordUtilities-1.0jca-1.7 特性。 您也可以新增 appSecurity-2.0passwordUtilities-1.0jca-1.6。例如:
    <featureManager>
       <feature>appSecurity-2.0</feature>
       <feature>passwordUtilities-1.0</feature>
       <feature>jca-1.7</feature>
    </featureManager>
  2. server.xml 檔中配置 authData 元素。例如:
    <authData id="myAuthData" user="myUser" password="myPassword"/> <!-- password can also be encoded -->
    
    在配置內進行密碼編碼。
    您可以利用 securityUtility encode 指令來取得編碼值。
  3. 利用應用程式 Servlet 或 Enterprise Bean 中的 DefaultPrincipalMapping JAAS 登入環境定義項目名稱來執行程式化登入,依您所需要來取代對映別名。 例如:
    HashMap map = new HashMap();
    map.put(com.ibm.wsspi.security.auth.callback.Constants.MAPPING_ALIAS, "myAuthData"); // Replace value with your alias.
    CallbackHandler callbackHandler = new com.ibm.wsspi.security.auth.callback.WSMappingCallbackHandler(map, null);
    LoginContext loginContext = new LoginContext("DefaultPrincipalMapping", callbackHandler);
    loginContext.login();
    Subject subject = loginContext.getSubject();
    Set<javax.resource.spi.security.PasswordCredential> creds = subject.getPrivateCredentials(javax.resource.spi.security.PasswordCredential.class);
    PasswordCredential passwordCredential = creds.iterator().next();
    註: 為了簡化起見,不顯示錯誤處理。 如果要求的鑑別別名不存在,或形態異常,會傳回 javax.security.auth.login.LoginException
  4. PasswordCredential 取得使用者名稱和密碼。 例如:
    String userName = passwordCredential.getUserName();
    char[] password = passwordCredential.getPassword();
    // 用 userName 和 password 執行一些動作。
  5. 如果已啟用 Java 2 安全,應用程式必須獲授予 javax.security.auth.PrivateCredentialPermission。 例如,在應用程式的 META-INF/permissions.xml 檔中,授予存取 PasswordCredential 物件的許可權:
    <?xml version="1.0" encoding="UTF-8"?>
    <permissions xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/permissions_7.xsd" version="7">
    
      <permission>
        <class-name>javax.security.auth.PrivateCredentialPermission</class-name>
        <name>javax.resource.spi.security.PasswordCredential * "*"</name>
        <actions>read</actions>
      </permission>
    
      <!-- Other permissions -->
    
    </permissions>

    如需「Jaa 2 安全」的相關資訊,請參閱 Liberty: Java 2 安全


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



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