EJB アプリケーション用のプログラマチック API による開発
このトピックを使用して、エンタープライズ JavaBeans (EJB) アプリケーションをプログラマチックにセキュアにします。
このタスクについて
- IsCallerInRole (String rolename): Bean の呼び出し元に、ロール名で指定されているセキュリティー・ロールが認可されている場合は、true を戻します。 呼び出し元に指定のロールが認可されていない場合、または呼び出し元が認証されていない場合は、false を戻します。 指定のロールに、全員のアクセスが認可されている場合は、常に true を戻します。
- getCallerPrincipal: java.security を戻します。Bean 呼び出し元の名前を含むプリンシパル・オブジェクト。 呼び出し元が認証されていない場合は、認証されていない名前を含むプリンシパルを戻します。
ログイン・モジュールを使用可能にして、これらの呼び出しによって戻されるプリンシパル・クラスを指定できます。
isCallerInRole メソッドが使用されている場合は、このメソッドに渡されたロール名を含んでいるサブエレメントである role-name を持つデプロイメント記述子で、security-role-ref エレメントを宣言します。実際のロールはアプリケーションのアセンブリー段階で作成されるため、論理ロールをロール名として使用して、security-role-ref エレメントの記述において、アセンブラーに十分なヒントを提供し、そのロールを実際のロールにリンクすることができます。 アセンブリー時に、アセンブラーは role-link サブエレメントを作成して、role-name を実際のロールにリンク します。Rational® Application Developer などのアセンブリー・ツールを使用すれば、security-role-ref エレメントを作成することができます。また、アセンブリー・ツールを使用して、アセンブリー段階で security-role-ref エレメントを作成することもできます。
手順
- 必要なセキュリティー・メソッドを EJB モジュール・コードに追加します。
- isCallerInRole メソッドで使用されるすべてのロール名に対して、role-name フィールドを使って security-role-ref エレメントを作成します。 開発中に security-role-ref エレメントが作成されない場合は、必ずアセンブリー段階で作成してください。
タスクの結果
例
EJB アプリケーションで、独自の許可表 (外部リソースとユーザーとの間のマッピング) を使用して外部リソースにアクセスさせたり、外部リソースへのアクセスを制御したりさせたい場合は、 セキュリティー・ポリシーを宣言的に指定する Java EE セキュリティー・モデル機能を使用すると役立ちます。 この場合、getCallerPrincipal メソッドを使用して、呼び出し元の ID を取得します。そうすると、アプリケーションは自分の許可表を調べて許可を実行することができます。 呼び出し元の識別は、データベースなどの外部ソース、あるいは別のエンタープライズ Bean から、対応するユーザー情報を取得する場合にも役に立ちます。同様の方法で isCallerInRole メソッドを使用することができます。
<security-role-ref>
<description>Provide hints to assembler for linking this role-name to
actual role here<¥description>
<role-name>Mgr<¥role-name>
</security-role-ref>
<security-role-ref>
<description>Hints provided by developer to map role-name to role-link</description>
<role-name>Mgr</role-name>
<role-link>Manager</role-link>
</security-role-ref>
public class aSessionBean implements SessionBean {
.....
// SessionContext extends EJBContext. If it is entity bean use EntityContext
javax.ejb.SessionContext context;
// The following method will be called by the EJB container
// automatically
public void setSessionContext(javax.ejb.SessionContext ctx) {
context = ctx; // save the session bean's context
}
....
private void aBusinessMethod() {
....
// to get bean's caller using getCallerPrincipal()
java.security.Principal principal = context.getCallerPrincipal();
String callerId= principal.getName();
// to check if bean's caller is granted Mgr role
boolean isMgr = context.isCallerInRole("Mgr");
// use the above information in any way as needed by the
//application
....
}
....
}
@javax.annotation.security.DeclareRoles("Mgr")
@Stateless // annotation is used to indicate a session bean
public class aSessionBean implements MyBusinessInterface { //you don't have to extend sessionbean interface
.....
// SessionContext extends EJBContext. In EJB 3.0 use Resource annotation to inject context
@Resource
javax.ejb.SessionContext context; }
....
private void aBusinessMethod() {
....
// to get bean's caller using getCallerPrincipal()
java.security.Principal principal = context.getCallerPrincipal();
String callerId= principal.getName();
// to check if bean's caller is granted Mgr role
boolean isMgr = context.isCallerInRole("Mgr");
// use the above information in any way as needed by the
//application
....
}
....
}