このトピックを使用して、Enterprise JavaBeans (EJB) アプリケーションをプログラマチックにセキュアにします。
ログイン・モジュールを使用可能にして、これらの呼び出しによって戻されるプリンシパル・クラスを指定できます。
isCallerInRole メソッドが使用されている場合は、このメソッドに渡された役割名を含んでいるサブエレメントである role-name を持つデプロイメント記述子で、security-role-ref エレメントを宣言します。 実際の役割はアプリケーションのアセンブリー段階で作成されるため、論理役割を役割名として使用して、security-role-ref エレメントの記述において、アセンブラーに十分なヒントを提供し、その役割を実際の役割にリンクすることができます。 アセンブリー時に、アセンブラーは role-link サブエレメントを作成して、role-name を実際の役割にリンク します。Rational Application Developer (RAD) などのアセンブリー・ツールを使用すれば、security-role-ref エレメントを作成することができます。 また、アセンブリー・ツールを使用して、アセンブリー段階で security-role-ref エレメントを作成することもできます。
EJB アプリケーションが、独自の許可表 (外部リソースとユーザーとの間のマッピング) を使用して外部リソースにアクセスしたり、外部リソースへのアクセスを制御したりする場合は、セキュリティー・ポリシーを宣言的に指定する J2EE セキュリティー・モデル機能を使用することが役に立ちます。 この場合、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 .... } .... }