Spring— Acegi工作机制

Acegi如何工作
为了说明Acegi安全系统如何工作,我们设想一个使用Acegi的例子。通常,一个安全系统需要发挥作用,它必须完成以下的工作:

l 首先,系统从客户端请求中获得Principal和Credential;
2 然后系统认证Principal和Credential信息;
3 如果认证通过,系统取出Principal的授权信息;
4 接下来,客户端发起操作请求;
5 系统根据预先配置的参数检查Principal对于该操作的授权;
6 如果授权检查通过则执行操作,否则拒绝。

那么,Acegi安全系统是如何完成这些工作的呢?首先,我们来看看Acegi安全系统的认证和授权的相关类图:

图中绿色部分是安全拦截器的抽象基类,它包含有两个管理类,AuthenticationManager和AccessDecisionManager,如图中灰色部分。AuthenticationManager用于认证ContextHolder中的Authentication对象(包含了Principal,Credential和Principal的授权信息)
AccessDecissionManager则用于授权一个特定的操作。

下面来看一个MethodSecurityInterceptor的例子:

<bean id="bankManagerSecurity"
class="net.sf.acegisecurity.intercept.method.MethodSecurityInterceptor">
<property name="validateConfigAttributes">
<value>true</value>
</property>
<property name="authenticationManager">
<ref bean="authenticationManager"/>
</property>
<property name="accessDecisionManager">
<ref bean="accessDecisionManager"/>
</property>
<property name="objectDefinitionSource">
<value>
net.sf.acegisecurity.context.BankManager.delete*=
ROLE_SUPERVISOR,RUN_AS_SERVER
net.sf.acegisecurity.context.BankManager.getBalance=
ROLE_TELLER,ROLE_SUPERVISOR,BANKSECURITY_CUSTOMER,RUN_
</value>
</property>
</bean>

上面的配置文件中,MethodSecurityInterceptor是AbstractSecurityInterceptor的一个实现类。它包含了两个管理器,authenticationManager和accessDecisionManager。这两者的配置如下:

<bean id="authenticationDao"
class="net.sf.acegisecurity.providers.dao.jdbc.JdbcDaoImpl">
<property name="dataSource"><ref bean="dataSource"/></property>
</bean>
<bean id="daoAuthenticationProvider"
class="net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider">
<property name="authenticationDao"><ref bean="authenticationDao"/></property>
</bean>
<bean id="authenticationManager"
class="net.sf.acegisecurity.providers.ProviderManager">
<property name="providers">
<list><ref bean="daoAuthenticationProvider"/></list>
</property>
</bean>

<bean id="roleVoter" class="net.sf.acegisecurity.vote.RoleVoter"/>
<bean id="accessDecisionManager" class="net.sf.acegisecurity.vote.AffirmativeBased">
<property name="allowIfAllAbstainDecisions"><value>false</value></property>
<property name="decisionVoters">
<list><ref bean="roleVoter"/></list>
</property>
</bean>

准备工作做好了,现在我们来看看Acegi安全系统是如何实现认证和授权机制的。以使用HTTP BASIC认证的应用为例子,它包括下面的步骤:

1. 用户登录系统,Acegi从acegisecurity.ui子系统的安全拦截器(如BasicProcessingFilter)中得到用户的登录信息(包括Principal和Credential)并放入Authentication对象,并保存在ContextHolder对象中;
2. 安全拦截器将Authentication对象交给AuthenticationManager进行身份认证,如果认证通过,返回带有Principal授权信息的Authentication对象。此时ContextHolder对象的Authentication对象已拥有Principal的详细信息;
3. 用户登录成功后,继续进行业务操作;
4. 安全拦截器(bankManagerSecurity)收到客户端操作请求后,将操作请求的数据包装成安全管理对象(FilterInvocation或MethodInvocation对象);
5. 然后,从配置文件(ObjectDefinitionSource)中读出相关的安全配置参数ConfigAttributeDefinition;
6. 接着,安全拦截器取出ContextHolder中的Authentication对象,把它传递给AuthenticationManager进行身份认证,并用返回值更新ContextHolder的Authentication对象;
7. 将Authentication对象,ConfigAttributeDefinition对象和安全管理对象(secure Object)交给AccessDecisionManager,检查Principal的操作授权;
8. 如果授权检查通过则执行客户端请求的操作,否则拒绝;

 

AccessDecisionVoter

注意上节的accessDecisionManager是一个AffirmativeBased类,它对于用户授权的投票策略是,只要通过其中的一个授权投票检查,即可通过;它的allowIfAllAbstainDecisions属性值是false,意思是如果所有的授权投票是都是弃权,则通不过授权检查。

Acegi安全系统包括了几个基于投票策略的AccessDecisionManager,上节的RoleVoter就是其中的一个投票策略实现,它是AccessDecisionVoter的一个子类。AccessDecisionVoter的具体实现类通过投票来进行授权决策,AccessDecisionManager则根据投票结果来决定是通过授权检查,还是抛出AccessDeniedException例外。

AccessDecisionVoter接口共有三个方法:

public int vote(Authentication authentication, Object object,

ConfigAttributeDefinition config);
public boolean supports(ConfigAttribute attribute);
public boolean supports(Class clazz);

其中的vote方法返回int返回值,它们是AccessDecisionVoter的三个静态成员属性:ACCESS_ABSTAIN,ACCESS_DENIED和ACCESS_GRANTED,它们分别是弃权,否决和赞成。

Acegi安全系统中,使用投票策略的AccessDecisionManager共有三个具体实现类:AffirmativeBased、ConsensusBased和UnanimousBased。它们的投票策略是,AffirmativeBased类只需有一个投票赞成即可通过;ConsensusBased类需要大多数投票赞成即可通过;而UnanimousBased类需要所有的投票赞成才能通过。

RoleVoter类是一个Acegi安全系统AccessDecisionVoter接口的实现。如果ConfigAttribute以ROLE_开头,RoleVoter则进行投票。如果GrantedAuthority的getAutority方法的String返回值匹配一个或多个以ROLE_开头的ConfigAttribute,则投票通过,否则不通过。如果没有以ROLE_开头的ConfigAttribute,RoleVoter则弃权。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值