WebSphere Application Server, Version 6.1   
             オペレーティング・システム: AIX , HP-UX, Linux, Solaris, Windows, Windows Vista

             目次と検索結果のパーソナライズ化

例: Enterprise Bean アプリケーションのコード

次の Enterprise JavaBeans (EJB) コンポーネントの例は、 EJB モジュール内の isCallerInRole および getCallerPrincipal メソッドの使用法 を示しています。

宣言セキュリティーを使用することをお勧めします。以下の サンプルは、isCallerInRole メソッドと getCallerPrincipal メソッドを使用した 1 つの方法です。 アプリケーションは、この結果がアプリケーションに対応している限り、これを使用できます。

File : Hello.java

package tests;
import java.rmi.RemoteException;
/**
 * Remote interface for Enterprise Bean: Hello
 */
public interface Hello extends javax.ejb.EJBObject {
      public abstract String getMessage()throws RemoteException;
    	public abstract void setMessage(String s)throws RemoteException;
}

File : HelloHome.java
package tests;
/**
 * Home interface for Enterprise Bean: Hello
 */
public interface HelloHome extends javax.ejb.EJBHome {
	/**
	 * Creates a default instance of Session Bean: Hello
	 */
	public tests.Hello create() throws javax.ejb.CreateException, java.rmi.RemoteException;
}

File : HelloBean.java

package tests;
/**
 * Bean implementation class for Enterprise Bean: Hello
 */
public class HelloBean implements javax.ejb.SessionBean {
	private javax.ejb.SessionContext mySessionCtx;
	/**
	 * getSessionContext
	 */
	public javax.ejb.SessionContext getSessionContext() {
	return mySessionCtx;
	}
	/**
	 * setSessionContext
	 */
	public void setSessionContext(javax.ejb.SessionContext ctx) {
          mySessionCtx = ctx;
	}
	/**
	 * ejbActivate
	 */
	public void ejbActivate() {
	}
	/**
	 * ejbCreate
	 */
	public void ejbCreate() throws javax.ejb.CreateException {
	}
	/**
	 * ejbPassivate
	 */
	public void ejbPassivate() {
	}
	/**
	 * ejbRemove
	 */
	public void ejbRemove() {
	}
	
	public java.lang.String message;
	
	
      //business methods

      // all users can call getMessage()
      public String getMessage() {
          return message;
      }

      // all users can call setMessage() but only few users can set new message.
      public void setMessage(String s) {
    	
        // get  bean's caller using getCallerPrincipal()
        java.security.Principal principal = mySessionCtx.getCallerPrincipal();     
        java.lang.String  callerId= principal.getName();

        // check if  bean's caller is granted Mgr role
        boolean isMgr = mySessionCtx.isCallerInRole("Mgr");

        // only set supplied message if caller is "bob" or caller is granted Mgr role
        if ( isMgr || callerId.equals("bob") )
            message = s;
        else
            message = "Hello";
      }

}
エンティティー Bean の開発後に、セッション Bean Hello のデプロイメント記述子内に、 セキュリティー役割の参照を作成します。
<security-role-ref>
     <description>Only Managers can call setMessage() on this bean (Hello)</description>
     <role-name>Mgr</role-name>
</security-role-ref>

<security-role-ref> エレメントの作成方法については、エンタープライズ Bean アプリケーションの保護 を参照してください。 「Map security-role-ref and role-name to role-link」の情報を使用してエレメントを作成します。




関連タスク
エンタープライズ Bean アプリケーションの保護
参照トピック    

ご利用条件 | フィードバック

最終更新: Jan 21, 2008 5:05:53 PM EST
http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=/com.ibm.websphere.base.doc/info/aes/ae/xsec_ejb.html