开发安全应用程序(四)-- 开发受保护的企业 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>
当只有声明性安全性不足以表述应用程序的安全性模型时,具有安全性意识的应用程序使用程序性安全性。程序性安全性由下列步骤组成:
在 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>
相关文章推荐
- spring,springMVC的优点和区别 spring 是是一个开源框架,是为了解决企业应用程序开发,功能如下 ◆目的:解决企业应用开发的复杂性 ◆功能:使用基本的JavaBean代替EJB,并
- iOS应用程序安全(25)-iOS开发安全编程实践
- 阿里云企业邮件使用安全及DirectMail开发
- ASP.NET安全[开发ASP.NET MVC应用程序时值得注意的安全问题](转)
- 学习《spring 3.x企业应用开发实战》之在IoC容器中装配Bean
- 《精通Spring4.X企业应用开发实战》读后感第五章(FactoryBean)
- ASP.NET企业开发框架IsLine FrameWork系列之十二--使用Session、Cookie与安全支持
- 开发安全应用程序(三)-- 开发表单登录页面
- 开发安全的ASP.NET应用程序
- 先有鸡还是先有蛋?应用程序开发与安全
- J2EE Java黑客大曝光:开发安全的Java应用程序
- 开发Web应用程序应注意的安全
- EJB通过ANT提高EJB应用程序的开发效率、无状态发展本地接口bean、开发状态bean
- 源代码安全是软件开发企业健康发展的核心要务
- 企业应用程序安全的新「守护神」
- 开发安全应用程序(五)-- 使用 JAAS 进行开发以使用程序来进行登录
- 移动应用程序开发应考虑的八大安全问题
- iOS开发-进阶:发布 在企业内部分发 iOS 应用程序
- 用NetBeans5.5加速企业应用程序开发
- 《精通Spring4.X企业应用开发实战》读后感第四章(Application中Bean的生命周期)