認証データを取得するためのプログラマチック・ログインの開発

Java 認証・承認サービス (JAAS) ログイン・フレームワークを使用して、アプリケーションから認証データを取得できます。

このタスクについて

アプリケーションは、DefaultPrincipalMapping JAAS コンテキスト・エントリー名を使用して JAAS プログラマチック・ログインを実行し、authData エレメントに構成されたユーザー名とパスワードが入ったプライベート・クレデンシャル・セット内の javax.resource.spi.security.PasswordCredential インスタンスで Subject オブジェクトを取得できます。

手順

  1. server.xml ファイルに appSecurity-2.0passwordUtilities-1.0、および jca-1.7 のフィーチャーを追加します。 appSecurity-2.0passwordUtilities-1.0、および jca-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 -->
    
    構成内でパスワードをエンコードします。You can get the encoded value by using the securityUtility encode command.
  3. マッピング別名を必要な別名に置換して、アプリケーション・サーブレットまたはエンタープライズ 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();
    // Do something with the userName and 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>

    Java 2 セキュリティーの有効化について詳しくは、 『Liberty: Java 2 セキュリティー』を参照してください。


トピックのタイプを示すアイコン タスク・トピック



タイム・スタンプ・アイコン 最終更新: Tuesday, 6 December 2016
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