Web アプリケーション用のプログラマチック・セキュリティー API による 開発

この情報を使用して、Web アプリケーションの API をプログラマチックに保護します。

始める前に

プログラマチック・セキュリティーは、 宣言セキュリティー単独ではアプリケーションのセキュリティー・モデルを表現するのに十分でない場合に、 セキュリティーを重視するアプリケーションによって使用されます。

認証、ログイン、ログアウト、getRemoteUser、 isUserInRole、および getAuthType の各サーブレット・セキュリティー・メソッドは、 javax.servlet.http.HttpServletRequest インターフェースのメソッドです。これらのサーブレット・セキュリティー・メソッドについて詳しくは、サーブレット・セキュリティー・メソッドの説明を参照してください。
注:

ログアウト、ログイン、および認証の各 API は、 このリリースの WebSphere® Application Server の Java™ Servlet 3.0 での新規 API です。

Web クライアントが保護された URI および保護されていない URI とどのように 相互作用するかを決定する Web 認証のいくつかのオプションを構成することができます。また、HTTPS クライアントの証明書認証が失敗した場合に、WebSphere Application Server が Web クライアントに基本認証情報を要求するかどうかを指定できます。詳しくは、『認証メカニズムの選択』を参照してください。

[z/OS]ログイン・モジュールを使用可能にして、これらの呼び出しによって戻されるプリンシパル・クラスを指定できます。 詳しくは、Java 認証・承認サービス・ログイン・モジュールを使用した、レジストリー・プリンシパルから System Authorization Facility ユーザー ID へのマッピングについての トピックを参照してください。

isUserInRole メソッドが使用されている場合は、このメソッドに渡される ロール名を含む role-name サブエレメントを持つデプロイメント記述子で、 あるいは @DeclareRoles アノテーションが指定されたデプロイメント記述子で、security-role-ref エレメントを宣言します。実際のロールはアプリケーションの アセンブリー段階で作成されるため、論理的ロールをロール名として使用して、security-role-ref エレメントの 記述において、アセンブラーに十分なヒントを提供し、 そのロールを実際のロールにリンクすることができます。アセンブリー時に、 アセンブラーは role-link サブエレメントを作成して、ロール名を実際のロールにリンク します。Rational® Application Developer などのアセンブリー・ツールを使用すれば、security-role-ref エレメントを作成することができます。また、アセンブリー・ツールを使用して、 アセンブリー段階で security-role-ref エレメントを作成することもできます。

手順

  1. 必要なセキュリティー・メソッドをサーブレット・コードに追加します。
  2. role-name フィールドを持つ security-role-ref エレメントを作成します。 開発中に security-role-ref エレメントが作成されない場合は、必ずアセンブリー段階で作成してください。

タスクの結果

サーブレット・アプリケーションをプログラマチックに保護しました。

これらのステップは、アプリケーションをプログラマチックに保護する場合に必要です。 この処置は、特に、Web アプリケーションが独自の許可表 (外部リソースの リモート・ユーザーへのマッピング) を使用して、外部リソースにアクセスしたり、 外部リソースへのアクセスを制御する場合に、役立ちます。 この場合、getUserPrincipal メソッド または getRemoteUser メソッドを使用して、リモート・ユーザーを取得してください。そうすると、アプリケーションはそれ自体の許可表を調べて許可を実行することができます。 リモート・ユーザー情報は、対応するユーザー情報を、データベースやエンタープライズ Bean などの外部ソースから取り出すときにも役に立ちます。 同じように、isUserInRole メソッドを使用できます。
作成後、security-role-ref エレメントを以下のように作成することができます。
<security-role-ref>
   <description>Provide hints to assembler for linking this role 
                name to an actual role here<¥description>
   <role-name>Mgr<¥role-name>
</security-role-ref>
アセンブリー時に、アセンブ ラーは、以下に示すように role-link を作成します。
<security-role-ref>
   <description>Hints provided by developer to map the role 
                name to the role-link</description>
   <role-name>Mgr</role-name>
   <role-link>Manager</role-link>
</security-role-ref>
プログラマチック・サーブレット・セキュリティー・メソッドは、 サーブレットの doGet、doPost、doPut、doDelete のいずれのサービス・メソッド内 にも追加できます。以下では、プログラマチック・セキュリティー API を使用した例を示しています。
public void doGet(HttpServletRequest request, 
HttpServletResponse response) {

   ....
   // to logoff the current user
	 request.logout();
    
   // to login with a new user
   request.login(“bob”,”bobpwd”)

   // to get remote user using getUserPrincipal()
   java.security.Principal principal = request.getUserPrincipal();
   String remoteUser = principal.getName();
 
   // to get remote user using getRemoteUser()
   remoteUser = request.getRemoteUser();

   // to check if remote user is granted Mgr role
   boolean isMgr = request.isUserInRole("Mgr");

   // use this information in any way as needed by 
   // the application 
   ....
                  
}
サーブレットの doGet、doPost、doPut および doDelete のいずれのサービス・メソッド内 でも、ユーザー ID とパスワードによるプログラマチック・ログインが可能です。以下に、プログラマチック・ログイン/ログアウト API を使用した例を示します。
public void doGet(HttpServletRequest request, 
HttpServletResponse response) {

   ....
   // to logout the current user. If you are not already authenticate, then no need to call the logout() method.
   request.logout();
    
   // to login with a new user
   request.login(“utle”,”mypwd”)

   // the user utle subject now set on the thread and the LTPA SSO cookie is set in the response
   ....
                  
}
サーブレットの doGet、doPost、doPut および doDelete のいずれのサービス・メソッド内 でも、別の ID によるプログラマチックな認証が可能です。以下の例で、Web サーブレットが basicAuth を 使用するように構成されている場合、Web サーバーは応答コード 401 を返し、 ログイン・プロンプトが表示され、ユーザーが認証に必要なユーザー ID とパスワードを入力できます。 以下に、プログラマチック・ログイン/ログアウト API を使用した例を示します。
public void doGet(HttpServletRequest request, 
HttpServletResponse response) {

   ....
   // to logout the current user. If you are not already authenticate, then no need to call the logout() method.
    
   // to login with a new user
   request.authenticate(response);

   // the new user subject now set on the thread and the LTPA SSO cookie is set in the response
   ....
                  
}
Servlet 3.0 のモジュールを開発する場合、 isCallerInRole メソッドの rolename 引数の値は、デプロイメント記述子に security-role-ref エレメントを宣言する代わりに、Java アノテーションを使用して定義できます。
@javax.annotation.security.DeclareRoles("Mgr")
public void doGet(HttpServletRequest request, 
HttpServletResponse response) {

   ....

   // to get remote user using getUserPrincipal()
   java.security.Principal principal = request.getUserPrincipal();
   String remoteUser = principal.getName();
 
   // to get remote user using getRemoteUser()
   remoteUser = request.getRemoteUser();

   // to check if remote user is granted Mgr role
   boolean isMgr = request.isUserInRole("Mgr");

   // use this information in any way as needed by 
   // the application 
   ....
                  
}

以下のサンプルでは、プログラマチックなセキュリティー・モデルを使用する Web アプリケーションまたはサーブレットの例を示します。

この例は、使用方法の一例です。 ただし、これがプログラマチックなセキュリティー・モデルの唯一の使用法ではありません。 アプリケーションは、getUserPrincipal、isUserInRole() および getRemoteUser メソッドで戻される情報がそのアプリケーションに対応している限り、これらの情報を使用することができます。 できる限り、宣言セキュリティー・モデルを使用してください。

ファイル: HelloServlet.java

public class HelloServlet extends javax.servlet.http.HttpServlet {

		public void doPost(
				javax.servlet.http.HttpServletRequest request,
				javax.servlet.http.HttpServletResponse response)
				throws javax.servlet.ServletException, java.io.IOException {
	}
public void doGet(
				javax.servlet.http.HttpServletRequest request,
				javax.servlet.http.HttpServletResponse response)
				throws javax.servlet.ServletException, java.io.IOException {
			
        String s = "Hello";
  	
        	
        // get remote user using getUserPrincipal()
        java.security.Principal principal = request.getUserPrincipal();
        String remoteUserName = "";
        if( principal != null )
         	remoteUserName = principal.getName();
// get remote user using getRemoteUser()
        String remoteUser = request.getRemoteUser();

        // check if remote user is granted Mgr role
        boolean isMgr = request.isUserInRole("Mgr");

        // display Hello username for managers and bob. 
        if ( isMgr || remoteUserName.equals("bob") )
            s = "Hello " + remoteUserName;

		 		 String message =  "<html> ¥n" +
            				"<head><title>Hello Servlet</title></head>¥n" +
      						"<body> /n +"
								"<h1> "  +s+ </h1>/n " + 
				byte[] bytes = message.getBytes();
		
				// displays "Hello" for ordinary users 
        // and displays "Hello username" for managers and "bob".
        response.getOutputStream().write(bytes);
	}

}
サーブレットを作成した後は、以下の例で示す HelloServlet サーブレット用のセキュリティー・ロールの参照を作成できます。
<security-role-ref>
     <description> </description>
     <role-name>Mgr</role-name>
</security-role-ref>

次のタスク

アプリケーションの開発後、アセンブリー・ツールを使用してロールを作成し、 実際のロールを security-role-ref エレメント内のロール名にリンクしてください。アセンブリー・ツールを使用した Web アプリケーションの保護に関する情報を参照してください。

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



タイム・スタンプ・アイコン 最終更新: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tsec_web
ファイル名:tsec_web.html