您的位置:首页 > 其它

开发安全应用程序(四)-- 开发受保护的企业 Bean 应用程序

2007-07-26 17:17 417 查看
开发受保护的企业 Bean 应用程序
当只有声明性安全性不足以表述应用程序的安全性模型时,具有安全性意识的应用程序使用程序性安全性。程序性安全性由下列步骤组成:

在 EJB 模块代码中添加必需的安全性方法。
为 isCallerInRole() 方法中使用的所有角色名创建带有角色名字段的安全性角色引用元素。
有关示例,参见示例:企业 Bean 应用程序代码。

Copy code
示例:企业 Bean 应用程序代码
这 个企业 Bean 组件示例说明 isCallerInRole() 和 getCallerPrincipal() 方法在 EJB 模块中的使用。建议尽可能地使用声明性安全性。此示例演示了一种使用 isCallerInRole() 和 getCallerPrincipal() 结果的方法,这不是使用这些方法的唯一途径。应用程序可以以任何适合于应用程序的方式来使用此结果。

有关此代码示例的法律信息,参见代码示例不保证声明。

远程接口

// File : Hello.java
package tests;
import java.rmi.RemoteException;

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;
import java.rmi.RemoteException;

public interface HelloHome extends javax.ejb.EJBHome {

public tests.Hello create() throws javax.ejb.CreateException, RemoteException;
}
Bean 实现

// File : HelloBean.java
package tests;

public class HelloBean implements javax.ejb.SessionBean {
private javax.ejb.SessionContext mySessionCtx;

public javax.ejb.SessionContext getSessionContext() {
return mySessionCtx;
}

public void setSessionContext(javax.ejb.SessionContext ctx) {
mySessionCtx = ctx;
}

public void ejbActivate() {
}

public void ejbCreate() throws javax.ejb.CreateException {
}

public void ejbPassivate() {
}

public void ejbRemove() {
}

public java.lang.String message;

// Business methods

// all users can call getMessage()
public String getMessage() throws java.rmi.RemoteException {
return message;
}

// all users can call setMessage() but only few users can set new message.
public void setMessage(String s) throws java.rmi.RemoteException {

// 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 之后,在部署描述符中的 Hello 会话 Bean 下面创建安全性角色引用,如下所示:

<security-role-ref>
<description>Only Managers can call setMessage() on this bean (Hello)</description>
<role-name>Mgr</role-name>
</security-role-ref>

添加必需的安全性方法

javax.ejb.EJBContext 接口提供了下列方法,这些方法允许“Bean 提供程序”访问关于企业 Bean 的调用者的安全性信息:

IsCallerInRole(String rolename)
如果已将指定的安全性角色(由角色名指定)的权限授予 Bean 的调用者,则此方法返回 true。如果没有将指定的角色授权给调用者,或者没有认证调用者,则此方法返回 false。如果已将指定的角色的访问权授权给每个人,则此方法始终返回 true。

getCallerPrincipal()
此方法返回包含 Bean 的调用者名的 java.security.Principal 对象。如果未认证调用者,则此方法返回包含未经认证的名称的主体。

可以在企业 Bean 的商业方法中添加程序企业 Bean 组件安全性方法 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
...
}
...
}
创建安全性角色引用元素

此步骤是有计划地保护应用程序所必需的。如果在开发期间没有创建安全性角色引用,则确保在汇编阶段创建它。

使 用 isCallerInRole() 方法时,应在部署描述符中使用包含传送给此方法的角色名的角色名元素来描述安全性角色引用。因为实际的角色是在应用程序的汇编期间创建的,所以您可使用逻 辑角色作为角色名,并在安全角色元素的描述中为汇编工具提供足够的线索以将该角色与实际角色链接。在汇编期间,汇编工具创建角色链接子元素以将角色名与实 际角色链接。如果使用诸如 WebSphere Studio Application Developer 之类的开发工具,则创建安全性角色引用元素是有可能的。也可以在汇编阶段使用汇编工具来创建安全性角色引用元素。

强烈建议不要在应用程序中硬编码安全性策略。鼓励您尽可能使用 J2EE 安全性 API 并描述性地指定安全性策略。这些 API 可用来开发具有安全性意识的企业 Bean 应用程序。

在 以下示例中,声明性安全性非常有用:您想要某个企业 Bean 应用程序访问外部资源,并通过它自已的授权表(外部资源到用户的映射)来控制对这些资源的访问。在这种情况下,可使用 getCallerPrincipal() 来获取调用者的身份,然后应用程序可查阅它自已的授权表来执行授权。调用者标识还可用来从外部源(如数据库)或从另一个企业 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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐