当前位置: 首页 >> 应用软件 >> 网络相关 >> Acegi安全系统详解
 

Acegi安全系统详解

作者:      来源:http://blog.csdn.net/HuDon/     发表时间:2007-02-13     浏览次数:      字号:    


2) daoAuthenticationProvider
  进行简单的基于数据库的身份验证。DaoAuthenticationProvider获取数据库中的账号密码并进行匹配,若成功则在通过用户身份的同时返回一个包含授权信息的Authentication对象,否则身份验证失败,抛出一个AuthenticatiionException

1<bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">        
2    <property name="userDetailsService" ref="jdbcDaoImpl"/>        
3    <property name="userCache" ref="userCache"/>        
4    <property name="passwordEncoder" ref="passwordEncoder"/>   
5</bean>






























































3) passwordEncoder
  使用加密器对用户输入的明文进行加密。Acegi提供了三种加密器:
1a :  PlaintextPasswordEncoder—默认,不加密,返回明文.
2b : ShaPasswordEncoder—哈希算法(SHA)加密
3c : Md5PasswordEncoder—消息摘要(MD5)加密


1<bean id="passwordEncoder" class="org.acegisecurity.providers.encoding.Md5PasswordEncoder"/>


4) jdbcDaoImpl
  用于在数据中获取用户信息。 acegi提供了用户及授权的表结构,但是您也可以自己来实现。通过usersByUsernameQuery这个SQL
得到你的(用户ID,密码,状态信息);通过authoritiesByUsernameQuery这个SQL得到你的(用户ID,授权信息)
 1<bean id="jdbcDaoImpl" class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl">
 2    <property name="dataSource" ref="dataSource"/>
 3    <property name="usersByUsernameQuery">    
 4        <value>select loginid,passwd,1 from users where loginid = ?</value>
 5    </property>       
 6    <property name="authoritiesByUsernameQuery">    
 7        <value>  
 8             select u.loginid,p.name from users u,roles r,permissions p,  
 9             user_role ur,role_permis rp where u.id=ur.user_id and   
10             r.id=ur.role_id and p.id=rp.permis_id and r.id=rp.role_id and    
11             p.status='1' and u.loginid=?    
12        </value>
13    </property>
14</bean>

5) userCache &  resourceCache

  缓存用户和资源相对应的权限信息。每当请求一个受保护资源时,daoAuthenticationProvider就会被调用以获取用户授权信息。如果每次都从数据库获取的话,那代价很高,对于不常改变的用户和资源信息来说,最好是把相关授权信息缓存起来。(详见 2.6.3 资源权限定义扩展 )
userCache提供了两种实现: NullUserCacheEhCacheBasedUserCache, NullUserCache实际上就是不进行任何缓存,EhCacheBasedUserCache是使用Ehcache来实现缓功能。

 1<bean id="userCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
 2    <property name="cacheManager" ref="cacheManager"/>
 3    <property name="cacheName" value="userCache"/>
 4</bean>
 5
 6<bean id="userCache" 
          class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache" autowire="byName">
 7    <property name="cache" ref="userCacheBackend"/>  
 8</bean>
 9
10<bean id="resourceCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
11    <property name="cacheManager" ref="cacheManager"/>
12    <property name="cacheName" value="resourceCache"/>
13</bean>
14
15<bean id="resourceCache" 
         class="org.springside.modules.security.service.acegi.cache.ResourceCache" autowire="byName">
16    <property name="cache" ref="resourceCacheBackend"/>
17</bean>




























6) basicProcessingFilter
  
用于处理HTTP头的认证信息,如从Spring远程协议(如Hessian和Burlap)或普通的浏览器如IE,Navigator的HTTP头中获取用户
信息,将他们转交给通过authenticationManager属性装配的认证管理器。如果认证成功,会将一个Authentication对象放到会话中
,否则,如果认证失败,会将控制转交给认证入口点(通过authenticationEntryPoint属性装配)
1<bean id="basicProcessingFilter" class="org.acegisecurity.ui.basicauth.BasicProcessingFilter">
2    <property name="authenticationManager" ref="authenticationManager"/>
3    <property name="authenticationEntryPoint" ref="basicProcessingFilterEntryPoint"/>
4</bean>

7) basicProcessingFilterEntryPoint
  通过向浏览器发送一个HTTP401(未授权)消息,提示用户登录。
处理基于HTTP的授权过程, 在当验证过程出现异常后的"去向",通常实现转向、在response里加入error信息等功能。

1 <bean id="basicProcessingFilterEntryPoint" 
2            class="org.acegisecurity.ui.basicauth.BasicProcessingFilterEntryPoint">   
3    <property name="realmName" value="SpringSide Realm"/>
4</bean>
 

8) authenticationProcessingFilterEntryPoint
  
     当抛出AccessDeniedException时,将用户重定向到登录界面。属性loginFormUrl配置了一个登录表单的URL,当需要用户登录时,authenticationProcessingFilterEntryPoint会将用户重定向到该URL
 

1<bean id="authenticationProcessingFilterEntryPoint" 
2      class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">        
3    <property name="loginFormUrl">            
4         <value>/security/login.jsp</value>        
5    </property>   
6    <property name="forceHttps" value="false"/>
7</bean>

 

2.2.3 HTTP安全请求

1) httpSessionContextIntegrationFilter
  每次request前 HttpSessionContextIntegrationFilter从Session中获取Authentication对象,在request完后, 又把Authentication对象保存到Session中供下次request使用,此filter必须其他Acegi filter前使用,使之能跨越多个请求。 

1<bean id="httpSessionContextIntegrationFilter" 
2                class="org.acegisecurity.context.HttpSessionContextIntegrationFilter"/>

2) httpRequestAccessDecisionManager
  经过投票机制来决定是否可以访问某一资源(URL方法)。allowIfAllAbstainDecisions为false时如果有一个或以上的decisionVoters投票通过,则授权通过。可选的决策机制有ConsensusBased和UnanimousBased

1<bean id="httpRequestAccessDecisionManager" class="org.acegisecurity.vote.AffirmativeBased">
2    <property name="allowIfAllAbstainDecisions" value="false"/>
3    <property name="decisionVoters">  
4        <list>
5            <ref bean="roleVoter"/>  
6        </list>
7    </property>
8</bean>

3) roleVoter
   必须是以rolePrefix设定的value开头的权限才能进行投票,如AUTH_ , ROLE_

1<bean id="roleVoter" class="org.acegisecurity.vote.RoleVoter">
2    <property name="rolePrefix" value="AUTH_"/>   
3</bean>

4)exceptionTranslationFilter
  异常转换过滤器,主要是处理AccessDeniedException和AuthenticationException,将给每个异常找到合适的"去向" 

1<bean id="exceptionTranslationFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter">  
2    <property name="authenticationEntryPoint" ref="authenticationProcessingFilterEntryPoint"/> 
3</bean>

5) authenticationProcessingFilter
  和servlet spec差不多,处理登陆请求.当身份验证成功时,AuthenticationProcessingFilter会在会话中放置一个Authentication对象,并且重定向到登录成功页面
         authenticationFailureUrl定义登陆失败时转向的页面
         defaultTargetUrl定义登陆成功时转向的页面
         filterProcessesUrl定义登陆请求的页面
         rememberMeServices用于在验证成功后添加cookie信息

 1<bean id="authenticationProcessingFilter" 
               class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
 2    <property name="authenticationManager" ref="authenticationManager"/>
 3    <property name="authenticationFailureUrl">
 4        <value>/security/login.jsp?login_error=1</value>
 5    </property>
 6    <property name="defaultTargetUrl">
 7        <value>/admin/index.jsp</value>
 8    </property>
 9    <property name="filterProcessesUrl">
10        <value>/j_acegi_security_check</value>
11    </property>
12    <property name="rememberMeServices" ref="rememberMeServices"/>
13</bean>

6) filterInvocationInterceptor

  在执行转向url前检查objectDefinitionSource中设定的用户权限信息。首先,objectDefinitionSource中定义了访问URL需要的属性信息(这里的属性信息仅仅是标志,告诉accessDecisionManager要用哪些voter来投票)。然后,authenticationManager掉用自己的provider来对用户的认证信息进行校验。最后,有投票者根据用户持有认证和访问url需要的属性,调用自己的voter来投票,决定是否允许访问。

1<bean id="filterInvocationInterceptor" class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
2    <property name="authenticationManager" ref="authenticationManager"/>
3    <property name="accessDecisionManager" ref="httpRequestAccessDecisionManager"/>
4    <property name="objectDefinitionSource" ref="filterDefinitionSource"/>
5</bean>


7)filterDefinitionSource(详见 2.6.3 资源权限定义扩展)
  自定义DBFilterInvocationDefinitionSource从数据库和cache中读取保护资源及其需要的访问权限信息 

1<bean id="filterDefinitionSource" 
2  class="org.springside.modules.security.service.acegi.DBFilterInvocationDefinitionSource">        
3    <property name="convertUrlToLowercaseBeforeComparison" value="true"/>        
4    <property name="useAntPath" value="true"/>        
5    <property name="acegiCacheManager" ref="acegiCacheManager"/>
6</bean>









2.2.4 方法调用安全控制

 

(详见 2.6.3 资源权限定义扩展)

1) methodSecurityInterceptor
  在执行方法前进行拦截,检查用户权限信息

1<bean id="methodSecurityInterceptor" 
2        class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">        
3    <property name="authenticationManager" ref="authenticationManager"/>        
4    <property name="accessDecisionManager" ref="httpRequestAccessDecisionManager"/>        
5    <property name="objectDefinitionSource" ref="methodDefinitionSource"/>
6</bean>


2) methodDefinitionSource
  自定义MethodDefinitionSource从cache中读取权限

1<bean id="methodDefinitionSource" 
2      class="org.springside.modules.security.service.acegi.DBMethodDefinitionSource">        
3    <property name="acegiCacheManager" ref="acegiCacheManager"/>    
4</bean>

[1] [2]

责任编辑 webmaster

 
 
 
 
 
评论更多>>
 
kan kan
 
 
发表
 
姓名: QQ:
性别: MSN:
E-mail: 主页:
评分: 1 2 3 4 5
评论内容:
验证码:
  
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。
  •