Let's go inside

this blog is deprecated as a result of laziness.
posts - 59, comments - 2, trackbacks - 0, articles - 0

TrailBlazer第14天--Security

Posted on 2006-07-28 19:19 Earth 阅读(193) 评论(0)  编辑  收藏 所属分类: JavaEE5/EJB3

The password files  密码文件

在任何的安全配置中,首先你得告诉应用程序在哪里可以找到用户名和密码以及角色。你可以使用LDAP服务器和数据库服务器来存储这类的信息。最简单的情况是在应用的classpath中使用简单的文本文件。下面是一个user.properties文件,它存储的是用户和密码对。在这个例子中有两个账号分别为admin和user.

admin=jboss
user=ejb3
下面是一个roles.properties文件。它存储了用户和角色之间的关系。每一个用户都可以拥有多个角色。比如用户admin就同时拥有AdminUser和RegularUser两种角色。
admin=AdminUser,RegularUser
user=RegularUser

The sample application  示例程序
 作为一个例子,让我们试着做一个有密码保护功能的投资计算器程序。为了使用这个计算器,你需要用user或者admin的身份登陆。我建议你此时还是使用user登陆(密码是ejb3)

RegularUser可以使用计算器,只有AdminUser有权增加更多的funds和investor profiles。所以,如果使用user登陆并尝试运行下面的两个程序,你将遭遇安全异常。然后你可以试着以admin的身份再尝试一遍(密码是jboss)
 
Security annotations  安全标注
 在EJB 3.0中, 你可以使用安全标注来指定在哪里可以找到密码/角色的列表,并且还可以指定哪种角色的用户允许访问这个方法。在下面的例子中,@SecurityDomain("other")表示在当前classpath下的.properties文件 是用来认证(authentication)的。@RolesAllowed表示哪些角色可以访问这种方法。用@PermitAll标注的方法则不受安全约束。

@Stateless
@SecurityDomain(
" other " )
public   class  SecureCalculator  implements  Calculator {
  @RolesAllowed({
" AdminUser " })
  
public   void  addFund (String name,  double  growthrate) {
    
//   
  }
  @RolesAllowed({
" AdminUser " })
  
public   void  addInvestor (String name,  int  start,  int  end) {
    
//   
  }
  @PermitAll
  
public  Collection  < Fund >  getFunds () {
    
//   
  }
  
//   
  @RolesAllowed({ " RegularUser " })
  
public   double  calculate ( int  fundId,  int  investorId, 
                                       
double  saving) {
    
//   
  }
}

The web layer configuration  web层的配置
 如果你是通过web页面登陆到系统,你的身份则会被应用的web层捕获,然后传递到EJB 3.0的中间层。你不得不配置web.xml文件来声明哪些页面受密码保护以及这些页面允许哪些角色访问。下面就是web.xml文件的内容。

< web-app >
  
< display-name > EJB3Trail </ display-name >

  
< security-constraint >
      
< web-resource-collection >
         
< web-resource-name >
            The Protected Calculator
         
</ web-resource-name >
         
< url-pattern > services/security/addfund.jsp </ url-pattern >
         
< url-pattern > services/security/addinvestor.jsp </ url-pattern >
         
< url-pattern > services/security/calculator.jsp </ url-pattern >
      
</ web-resource-collection >

      
< auth-constraint >
         
< role-name > AdminUser </ role-name >
         
< role-name > RegularUser </ role-name >
      
</ auth-constraint >

      
< user-data-constraint >
         
< transport-guarantee > NONE </ transport-guarantee >
      
</ user-data-constraint >
   
</ security-constraint >

   
< security-role >
      
< description > Authorized to access everything. </ description >
      
< role-name > AdminUser </ role-name >
   
</ security-role >
   
< security-role >
      
< description > Authorized to limited access. </ description >
      
< role-name > RegularUser </ role-name >
   
</ security-role >

   
< login-config >
      
< auth-method > FORM </ auth-method >
      
< form-login-config >
         
< form-login-page > security/login.html </ form-login-page >
         
< form-error-page > security/loginFailed.html </ form-error-page >
      
</ form-login-config >
   
</ login-config >

</ web-app >

只有注册用户登录后才能发表评论。


网站导航: