Java トークン・バリデーター

improve this page | report issue

概説

IBM Mobile Foundation は、外部リソースにセキュリティー機能を適用するための Java ライブラリーを提供しています。
Java ライブラリーは、JAR ファイル (mfp-java-token-validator-8.0.0.jar) として提供されます。

このチュートリアルでは、スコープ (accessRestricted) を使用して、単純な Java サーブレット GetBalance を保護する方法を示します。

前提条件:

フロー

.jar ファイル依存関係の追加

mfp-java-token-validator-8.0.0.jar ファイルは、maven dependency として使用できます。

<dependency>
  <groupId>com.ibm.mfp</groupId>
  <artifactId>mfp-java-token-validator</artifactId>
  <version>8.0.0</version>
</dependency>

TokenValidationManager のインスタンス化

トークンの検証を可能にするために、TokenValidationManager をインスタンス化します。

TokenValidationManager(java.net.URI authorizationURI, java.lang.String clientId, java.lang.String clientSecret);
  • authorizationURI: 許可サーバーの URI。通常は、MobileFirst Server です。 例えば、http://localhost:9080/mfp/api です。
  • clientId: MobileFirst Operations Console で構成した機密クライアント ID。
  • clientSecret: MobileFirst Operations Console で構成した機密クライアント秘密鍵。

このライブラリーは、許可サーバーのイントロスペクション・エンドポイントとの対話をカプセル化および簡素化する API を公開します。 詳細な API リファレンスについては、MobileFirst Java トークン・バリデーター API リファレンスを参照してください。

資格情報の検証

validate API メソッドは、許可ヘッダーの検証を許可サーバーに依頼します。

public TokenValidationResult validate(java.lang.String authorizationHeader, java.lang.String expectedScope);
  • authorizationHeader: Authorization HTTP ヘッダーのコンテンツ、すなわち、アクセス・トークンです。 例えば、HttpServletRequest (httpServletRequest.getHeader("Authorization")) から取得されます。
  • expectedScope: トークンを検証するための基準となるスコープ。例えば、accessRestricted です。

結果の TokenValidationResult オブジェクトを照会して、エラーまたは有効なイントロスペクション・データを確認できます。

TokenValidationResult tokenValidationRes = validator.validate(authCredentials, expectedScope);
if (tokenValidationRes.getAuthenticationError() != null) {
    // Error
    AuthenticationError error = tokenValidationRes.getAuthenticationError();
    httpServletResponse.setStatus(error.getStatus());
    httpServletResponse.setHeader("WWW-Authenticate", error.getAuthenticateHeader());
} else if (tokenValidationRes.getIntrospectionData() != null) {
    // Success logic here
}

イントロスペクション・データ

getIntrospectionData() によって返される TokenIntrospectionData オブジェクトは、現在のアクティブ・ユーザーのユーザー名など、クライアントに関する情報を提供します。

httpServletRequest.setAttribute("introspection-data", tokenValidationRes.getIntrospectionData());
TokenIntrospectionData introspectionData = (TokenIntrospectionData) request.getAttribute("introspection-data");
String username = introspectionData.getUsername();

キャッシュ

TokenValidationManager クラスには、トークンおよびイントロスペクション・データをキャッシュに入れる内部キャッシュが付いてきます。 キャッシュの目的は、同じヘッダーを使用して要求が発行された場合に、許可サーバーに対して行われるトークンのイントロスペクション の量を減らすことです。

デフォルトのキャッシュ・サイズは 50000 項目です。 この容量に達すると、一番古いトークンから削除されます。

TokenValidationManager のコンストラクターは、保管する cacheSize (イントロスペクション・データ項目の数) も受け入れます。

public TokenValidationManager(java.net.URI authorizationURI, java.lang.String clientId, java.lang.String clientSecret, long cacheSize);

単純な Java サーブレットの保護

  1. ハードコーディングされた値を返す、GetBalance という単純な Java サーブレットを作成します。

    @WebServlet("/GetBalance")
    public class GetBalance extends HttpServlet {
     	private static final long serialVersionUID = 1L;
    
     	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     		//Return hardcoded value
     		response.getWriter().append("17364.9");
     	}
    
    }
    
  2. JTVFilter という javax.servlet.Filter 実装を作成します。これは、指定されたスコープの許可ヘッダーを検証します。

    public class JTVFilter implements Filter {
    
     	public static final String AUTH_HEADER = "Authorization";
     	private static final String AUTHSERVER_URI = "http://localhost:9080/mfp/api"; //Set here your authorization server URI
     	private static final String CLIENT_ID = "jtv"; //Set here your confidential client ID
     	private static final String CLIENT_SECRET = "jtv"; //Set here your confidential client SECRET
    
     	private TokenValidationManager validator;
     	private FilterConfig filterConfig = null;
    
     	@Override
     	public void init(FilterConfig filterConfig) throws ServletException {
     		URI uri = null;
     		try {
     			uri = new URI(AUTHSERVER_URI);
     			validator = new TokenValidationManager(uri, CLIENT_ID, CLIENT_SECRET);
     			this.filterConfig = filterConfig;
     		} catch (Exception e1) {
     			System.out.println("Error reading introspection URI");
     		}
     	}
    
     	@Override
     	public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
     		String expectedScope = filterConfig.getInitParameter("scope");
     		HttpServletRequest httpServletRequest = (HttpServletRequest) req;
     		HttpServletResponse httpServletResponse = (HttpServletResponse) res;
    
     		String authCredentials = httpServletRequest.getHeader(AUTH_HEADER);
    
     		try {
     			TokenValidationResult tokenValidationRes = validator.validate(authCredentials, expectedScope);
     			if (tokenValidationRes.getAuthenticationError() != null) {
     				// Error
     				AuthenticationError error = tokenValidationRes.getAuthenticationError();
     				httpServletResponse.setStatus(error.getStatus());
     				httpServletResponse.setHeader("WWW-Authenticate", error.getAuthenticateHeader());
     			} else if (tokenValidationRes.getIntrospectionData() != null) {
     				// Success
     				httpServletRequest.setAttribute("introspection-data", tokenValidationRes.getIntrospectionData());
     				filterChain.doFilter(req, res);
     			}
     		} catch (TokenValidationException e) {
     			httpServletResponse.setStatus(500);
     		}
     	}
    
    }
    
  3. サーブレットの web.xml ファイル内に JTVFilter のインスタンスを宣言し、scope accessRestricted をパラメーターとして渡します。

    <filter>
       <filter-name>accessRestricted</filter-name>
       <filter-class>com.sample.JTVFilter</filter-class>
       <init-param>
         <param-name>scope</param-name>
         <param-value>accessRestricted</param-value>
       </init-param>
    </filter>
    

    サーブレットをこのフィルターで保護します。

    <filter-mapping>
       <filter-name>accessRestricted</filter-name>
       <url-pattern>/GetBalance</url-pattern>
    </filter-mapping>
    

サンプル・アプリケーション

サポートされるアプリケーション・サーバー (Tomcat、WebSphere Application Server フル・プロファイル、および WebSphere Application Server Liberty プロファイル) にプロジェクトをデプロイできます。
単純な Java サーブレットをダウンロードします。

サンプルの使用法

  1. MobileFirst Operations Console で、必ず機密クライアントと秘密鍵の値を更新してください。
  2. UserLogin または PinCodeAttempts のいずれかのセキュリティー検査をデプロイします。
  3. 一致するアプリケーションを登録します。
  4. accessRestricted スコープをセキュリティー検査にマップします。
  5. クライアント・アプリケーションを更新して、サーブレット URL に WLResourceRequest を発行します。
Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.
Last modified on February 28, 2020