EJB 应用程序的程序化 API 开发

使用本主题以编程方式保护 Enterprise JavaBeans (EJB) 应用程序。

关于此任务

当声明式安全性独自不足以表达应用程序的安全模型时,具有安全性意识的应用程序使用程序化安全性。 javax.ejb.EJBContext 应用程序编程接口 (API) 接口提供两个方法,由此 Bean 提供程序可以访问有关企业 Bean 调用者的安全性信息。
  • IsCallerInRole(String rolename):如果 bean 调用者被授予由角色名指定的安全角色,那么返回 true。如果未授予调用者指定的角色,或者如果未认证调用者,那么返回 false。如果指定的角色被授予“每个人”访问权,那么它总是返回 true
  • getCallerPrincipal:返回 java.security。包含 Bean 调用者名称的主体对象。如果未认证调用者,那么它返回包含未认证的名称的主体。

可以启用登录模块以指示这些调用返回了哪个主体类。

在使用 isCallerInRole 方法时,请在部署描述符中声明 security-role-ref 元素并使它的 role-name 子元素包含传递给此方法的角色名称。由于实际的角色是在应用程序组装阶段创建的,所以可以将逻辑角色用作角色名并在 security-role-ref 元素的描述中向组装者提供足够的线索,以将该角色链接到实际的角色。组装期间,组装者将创建 role-link 子元素,以将角色名链接到实际的角色。如果使用诸如 Rational® Application Developer 之类的组装工具,那么可以创建 security-role-ref 元素。您也可以在组装阶段使用组装工具创建 security-role-ref 元素。

过程

  1. 在 EJB 模块代码中添加必需的安全性方法。
  2. 为 isCallerInRole 方法中使用的所有角色名称,创建带有 role-name 字段的 security-role-ref 元素。 如果在开发期间未创建 security-role-ref 元素,请确保在组装阶段中创建该元素。

结果

执行上述步骤会产生程序化保护的 EJB 应用程序。

示例

强烈建议不要在应用程序中对安全策略进行硬编码。只要可能,就鼓励使用声明地指定安全策略的 Java™ Platform, Enterprise Edition (Java EE) 安全模型功能。使用这些 API 开发具有安全性意识的 EJB 应用程序。

当 EJB 应用程序希望访问外部资源,且希望使用其自己的授权表(外部资源到用户映射)来控制这些外部资源的访问时,非常有用的操作是使用 Java EE 安全模型功能以声明方式来指定安全策略。在这种情况下,请使用 getCallerPrincipal() 方法获取调用者标识,然后应用程序可以查询它自已的授权表来执行授权。调用者标识也可以帮助从外部源(如数据库)或从另一个企业 Bean 检索相应的用户信息。可以以类似的方式使用 isCallerInRole 方法。

在开发之后,可创建 security-role-ref 元素:
<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>
组装期间,组装者创建 role-link 元素:
<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>
可以在企业 Bean 的任何业务方法中添加程序化的 EJB 组件安全性方法(例如 isCallerInRole 和 getCallerPrincipal)。程序化安全性 API 的以下示例包括会话 Bean:
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 
                          
       ....
       }

       ....
}
当开发 EJB 3.x 模块时,可使用 Java 注释定义 isCallerInRole 方法中 rolename 自变量的值,而不是通过在部署描述符中声明 security-role-ref 元素来进行定义。
  @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 
                          
       ....
       }

       ....
}

下一步做什么

在开发应用程序之后,使用组装工具创建角色,并将实际的角色链接到 security-role-ref 元素中的角色名称。请参阅有关通过使用组装工具来保护 Web 应用程序的信息。

指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tsec_ejb
文件名:tsec_ejb.html