`
yangmage
  • 浏览: 823 次
  • 性别: Icon_minigender_1
  • 来自: 西安
最近访客 更多访客>>
社区版块
存档分类
最新评论

Acegi访问决策管理器的三种实现详解

 
阅读更多

关于Acegi在对Authentication(用户的权限组)与Resource的ConfigAttributeDefinition(资源权限组)的匹配问题一点解释.


问题提出:

最近在看Acegi配置时出现了一点问题:

在DB中配置了用户A有Role_User,Role_Admin两个权限,配置 一个URL可以使Role_Admin和Role_SuperAdmin访问.
按理来讲,A也有访问权限,但实际中老是出问题,细心看了一下AccessDessionManager的实现类,发现了问题所在:


Spring的处理流程:


1用户请求某一页面

2Spring Acegi Filter Chain根据Filter的拦截后缀拦截这一请求,并送到一系列的Filter中进行过滤.

3首次访问时,当然用户未登陆,此时Authentication是空的,(accessDecisionManager中判断SecurityContextHolder.getContext().getAuthentication()为空,发出一个未找到权限的Event并抛出异常.)

4exceptionTranslationFilter接到这个异常,并把请求转向到EntryPoint配置的登陆入口点(authenticationEntryPoint)

5.用户登陆.此时Filter Chain再次拦截到请求,并转给Filter们.

6.accessDecisionManager获取用户的Authentication组以及资源的ConfigAttributeDefinition权限列表.

7.当accessDecisionManager由UnanimousBased实现时,来看UnanimousBased的代码:
---------------------------------------------------------------

public void decide(Authentication authentication, Object object, ConfigAttributeDefinition config) 
    throws AccessDeniedException 
  { 
    int grant = 0; 
    int abstain = 0; 

    Iterator configIter = config.getConfigAttributes(); 

    while (configIter.hasNext()) { 
      ConfigAttributeDefinition thisDef = new ConfigAttributeDefinition(); 
      thisDef.addConfigAttribute((ConfigAttribute)configIter.next());      



     Iterator voters = getDecisionVoters().iterator(); 

      while (voters.hasNext()) { 
        AccessDecisionVoter voter = (AccessDecisionVoter)voters.next(); 
        int result = voter.vote(authentication, object, thisDef); //要求权限必须N对N的相等

        switch (result) 
        { 
        case 1: 
          ++grant; 

          break; 
        case -1: 
          throw new AccessDeniedException(this.messages.getMessage("AbstractAccessDecisionManager.accessDenied", "Access is denied")); 
        default: 
          ++abstain; 
        } 

      } 

    } 

    if (grant > 0) { 
      return; //有一张赞成票即可.
    } 

    checkAllowIfAllAbstainDecisions(); 
  } 

 
----------------------------------------------------------------------
可以发现,UnanimousBased要求[每个Voter都不拒绝用户权限与资源权限时才通过.同时必须:资源权限有N个,那么,用户权限也必须包含这N个时才通过.即相当于打开这个资源需要N把钥匙,用户必须同时拥有这N把钥匙才可以.]


接下来看AffirmativeBased 这个决策器的策略:

----------------------------------------------------------------------
Iterator iter = this.getDecisionVoters().iterator();
        int deny = 0;

        while (iter.hasNext()) {
            AccessDecisionVoter voter = (AccessDecisionVoter) iter.next();
            int result = voter.vote(authentication, object, config); //不要求权限必须相等

            switch (result) {
            case AccessDecisionVoter.ACCESS_GRANTED:
                return;

            case AccessDecisionVoter.ACCESS_DENIED:
                deny++;

                break;

            default:
                break;
            }
        }

        if (deny > 0) {
            throw new AccessDeniedException(messages.getMessage("AbstractAccessDecisionManager.accessDenied",
                    "Access is denied"));
        } //不能存在否决票

        // To get this far, every AccessDecisionVoter abstained
        checkAllowIfAllAbstainDecisions();
    }

------------------------------------------------------------------
可以看出,它只要求用户拥有资源权限N把钥匙中的任何一把,同时所有Voter赞成才可以访问即可以打开这个资源.

还有一个ConsensusBased :

-------------------------------------------------------------------
Iterator iter = this.getDecisionVoters().iterator();
        int grant = 0;
        int deny = 0;
        int abstain = 0;

        while (iter.hasNext()) {
            AccessDecisionVoter voter = (AccessDecisionVoter) iter.next();
            int result = voter.vote(authentication, object, config);

            switch (result) {
            case AccessDecisionVoter.ACCESS_GRANTED:
                grant++;

                break;

            case AccessDecisionVoter.ACCESS_DENIED:
                deny++;

                break;

            default:
                abstain++;

                break;
            }
        }

        if (grant > deny) {
            return;
        }

        if (deny > grant) {
            throw new AccessDeniedException(messages.getMessage("AbstractAccessDecisionManager.accessDenied",
                    "Access is denied"));
        }

        if ((grant == deny) && (grant != 0)) {
            if (this.allowIfEqualGrantedDeniedDecisions) {
                return;
            } else {
                throw new AccessDeniedException(messages.getMessage("AbstractAccessDecisionManager.accessDenied",
                        "Access is denied"));
            }
        }

        // To get this far, every AccessDecisionVoter abstained
        checkAllowIfAllAbstainDecisions();
------------------------------------------------------------
那么这个ConsensusBased 也只需要用户拥有任何一把钥匙,并且Voter们赞成票大于拒绝票时才能访问,当赞成与拒绝票相等时(没有弃权),会判断另一个参数来看配置中是否允许此种情况下访问.



end:可以看出,这三种管理器不仅在Voter的投票策略上是不同的,而且对于用户权限与资源权限配置存在多对多情况下的认证策略也有不同,UanimouseBased最为严格.

 

0
1
分享到:
评论

相关推荐

    Acegi学习笔记--Acegi详解实战Acegi实例

    4. **AccessDecisionManager**:决策管理器,根据策略判断用户是否被授权访问资源。 5. **RoleVoter**:基于角色的投票器,根据用户的角色决定是否允许访问。 三、Acegi的配置 1. **Web安全配置**:在web.xml中配置...

    acegi2.0

    Acegi 2.0的核心是过滤器链,它包含了多个安全相关的过滤器,如AuthenticationProcessingFilter用于处理登录请求,FilterSecurityInterceptor则用于执行访问控制决策。 4. **会话管理**: Spring Security提供了...

    springside 玩转acegi

    它的核心组件包括:Authentication Manager(认证管理器)、Access Decision Manager(访问决策管理器)和Filter Security Interceptor(过滤器安全拦截器)。 在`applicationContext-acegi-security.xml`配置文件...

    Spring Acegi

    4. **访问决策管理器(Access Decision Manager)**:处理授权决策,根据策略判断用户是否允许访问资源。 ### 二、Spring Acegi 功能 1. **支持多种认证机制**:如基于表单的身份验证、HTTP 基本认证、digest 验证...

    CAS 单点登录安装笔记3 -- 与acegi集成

    在这个文件中,我们需要配置Acegi的认证管理器(AuthenticationManager)和访问决策管理器(AccessDecisionManager),以指向CAS服务器进行身份验证。同时,配置CasProcessingFilter用于处理CAS的票据(ticket)验证...

    使用 Acegi 保护 Java 应用程序: 续二

    这个文件定义了安全策略,包括定义安全通道(secured-channel),设置访问决策管理器(AccessDecisionManager),以及配置认证提供者(AuthenticationProvider)等。 5. **Spring 集成**:Acegi 与 Spring 框架深度...

    acige包--acegi-security-1.0.7

    ACEGI使用`AccessDecisionManager`来执行授权决策,`AccessDecisionVoter`则是投票机制的核心,它可以根据不同的策略来决定是否允许访问。此外,`PreInvocationAuthorizationAdvice`和`...

    Packtpub.Spring.Security.3.May.2010.(PDF && CODE)

    4. **授权策略**:探讨权限分配和访问控制,包括基于角色的访问控制(RBAC)、表达式式访问控制(ACEGI)和访问决策投票器。同时,还会讨论如何定义和管理权限。 5. **过滤器链**:解释Spring Security的过滤器链...

    Spring框架应用的权限控制系统详解

    拦截器负责在请求处理前进行安全检查,而安全控制管理组件则包括AuthenticationManager(负责用户认证)、AccessDecisionManager(负责访问决策,即授权)和RunAsManager(用于角色切换)。 在实际应用中,我们需要...

Global site tag (gtag.js) - Google Analytics