Web アプリケーション用のプログラマチック・セキュリティー API による 開発
この情報を使用して、Web アプリケーションの API をプログラマチックに保護します。
始める前に
プログラマチック・セキュリティーは、 宣言セキュリティー単独ではアプリケーションのセキュリティー・モデルを表現するのに十分でない場合に、 セキュリティーを重視するアプリケーションによって使用されます。
ログアウト、ログイン、および認証の各 API は、 このリリースの WebSphere® Application Server の Java™ Servlet 3.0 での新規 API です。
Web クライアントが保護された URI および保護されていない URI とどのように 相互作用するかを決定する Web 認証のいくつかのオプションを構成することができます。また、HTTPS クライアントの証明書認証が失敗した場合に、WebSphere Application Server が Web クライアントに基本認証情報を要求するかどうかを指定できます。詳しくは、『認証メカニズムの選択』を参照してください。
ログイン・モジュールを使用可能にして、これらの呼び出しによって戻されるプリンシパル・クラスを指定できます。
詳しくは、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 エレメントを作成することもできます。
手順
- 必要なセキュリティー・メソッドをサーブレット・コードに追加します。
- role-name フィールドを持つ security-role-ref エレメントを作成します。 開発中に 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>
<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>
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
....
}
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
....
}
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
....
}
@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);
}
}
<security-role-ref>
<description> </description>
<role-name>Mgr</role-name>
</security-role-ref>