`
jiwenke
  • 浏览: 396920 次
  • 性别: Icon_minigender_1
  • 来自: 南京
博客专栏
4ee69929-b8e1-3eb5-bbca-157d075d6192
Spring技术内幕——深...
浏览量:181770
D1c226f7-74e9-3ec4-a15b-18a45ccd88c5
随笔:Spring与云计算...
浏览量:25484
社区版块
存档分类
最新评论

Spring源代码解析(十):Spring Acegi框架授权的实现

阅读更多
我们从FilterSecurityInterceptor我们从入手看看怎样进行授权的:
//这里是拦截器拦截HTTP请求的入口
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
        FilterInvocation fi = new FilterInvocation(request, response, chain);
        invoke(fi);
    }
//这是具体的拦截调用
    public void invoke(FilterInvocation fi) throws IOException, ServletException {
        if ((fi.getRequest() != null) && (fi.getRequest().getAttribute(FILTER_APPLIED) != null)
            && observeOncePerRequest) {
           //在第一次进行过安全检查之后就不会再做了
            fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
        } else {
            //这是第一次收到相应的请求,需要做安全检测,同时把标志为设置好 -  FILTER_APPLIED,下次就再有请求就不会作相同的安全检查了
            if (fi.getRequest() != null) {
                fi.getRequest().setAttribute(FILTER_APPLIED, Boolean.TRUE);
            }
            //这里是做安全检查的地方
            InterceptorStatusToken token = super.beforeInvocation(fi);
            //接着向拦截器链执行
            try {
                fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
            } finally {
                super.afterInvocation(token, null);
            }
        }
    }

我们看看在AbstractSecurityInterceptor是怎样对HTTP请求作安全检测的:
    protected InterceptorStatusToken beforeInvocation(Object object) {
        Assert.notNull(object, "Object was null");

        if (!getSecureObjectClass().isAssignableFrom(object.getClass())) {
            throw new IllegalArgumentException("Security invocation attempted for object "
                + object.getClass().getName()
                + " but AbstractSecurityInterceptor only configured to support secure objects of type: "
                + getSecureObjectClass());
        }
        //这里读取配置FilterSecurityInterceptor的ObjectDefinitionSource属性,这些属性配置了资源的安全设置
        ConfigAttributeDefinition attr = this.obtainObjectDefinitionSource().getAttributes(object);

        if (attr == null) {
            if(rejectPublicInvocations) {
                throw new IllegalArgumentException(
                      "No public invocations are allowed via this AbstractSecurityInterceptor. "
                    + "This indicates a configuration error because the "
                    + "AbstractSecurityInterceptor.rejectPublicInvocations property is set to 'true'");
            }

            if (logger.isDebugEnabled()) {
                logger.debug("Public object - authentication not attempted");
            }

            publishEvent(new PublicInvocationEvent(object));

            return null; // no further work post-invocation
        }


        if (logger.isDebugEnabled()) {
            logger.debug("Secure object: " + object.toString() + "; ConfigAttributes: " + attr.toString());
        }
        //这里从SecurityContextHolder中去取Authentication对象,一般在登录时会放到SecurityContextHolder中去
        if (SecurityContextHolder.getContext().getAuthentication() == null) {
            credentialsNotFound(messages.getMessage("AbstractSecurityInterceptor.authenticationNotFound",
                    "An Authentication object was not found in the SecurityContext"), object, attr);
        }

        // 如果前面没有处理鉴权,这里需要对鉴权进行处理
        Authentication authenticated;

        if (!SecurityContextHolder.getContext().getAuthentication().isAuthenticated() || alwaysReauthenticate) {
            try {//调用配置好的AuthenticationManager处理鉴权,如果鉴权不成功,抛出异常结束处理
                authenticated = this.authenticationManager.authenticate(SecurityContextHolder.getContext()
                                                                                             .getAuthentication());
            } catch (AuthenticationException authenticationException) {
                throw authenticationException;
            }

            // We don't authenticated.setAuthentication(true), because each provider should do that
            if (logger.isDebugEnabled()) {
                logger.debug("Successfully Authenticated: " + authenticated.toString());
            }
            //这里把鉴权成功后得到的Authentication保存到SecurityContextHolder中供下次使用
            SecurityContextHolder.getContext().setAuthentication(authenticated);
        } else {//这里处理前面已经通过鉴权的请求,先从SecurityContextHolder中去取得Authentication
            authenticated = SecurityContextHolder.getContext().getAuthentication();

            if (logger.isDebugEnabled()) {
                logger.debug("Previously Authenticated: " + authenticated.toString());
            }
        }

        // 这是处理授权的过程
        try {
            //调用配置好的AccessDecisionManager来进行授权
            this.accessDecisionManager.decide(authenticated, object, attr);
        } catch (AccessDeniedException accessDeniedException) {
            //授权不成功向外发布事件
            AuthorizationFailureEvent event = new AuthorizationFailureEvent(object, attr, authenticated,
                    accessDeniedException);
            publishEvent(event);

            throw accessDeniedException;
        }

        if (logger.isDebugEnabled()) {
            logger.debug("Authorization successful");
        }

        AuthorizedEvent event = new AuthorizedEvent(object, attr, authenticated);
        publishEvent(event);

        // 这里构建一个RunAsManager来替代当前的Authentication对象,默认情况下使用的是NullRunAsManager会把SecurityContextHolder中的Authentication对象清空
        Authentication runAs = this.runAsManager.buildRunAs(authenticated, object, attr);

        if (runAs == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("RunAsManager did not change Authentication object");
            }

            // no further work post-invocation
            return new InterceptorStatusToken(authenticated, false, attr, object);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("Switching to RunAs Authentication: " + runAs.toString());
            }

            SecurityContextHolder.getContext().setAuthentication(runAs);

            // revert to token.Authenticated post-invocation
            return new InterceptorStatusToken(authenticated, true, attr, object);
        }
    }

到这里我们假设配置AffirmativeBased作为AccessDecisionManager:
//这里定义了决策机制,需要全票才能通过
    public void decide(Authentication authentication, Object object, ConfigAttributeDefinition config)
        throws AccessDeniedException {
        //这里取得配置好的迭代器集合
        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"));
        }

        // 这里对弃权票进行处理,看看是全是弃权票的决定情况,默认是不通过,由allowIfAllAbstainDecisions变量控制
        checkAllowIfAllAbstainDecisions();
    }
具体的投票由投票器进行,我们这里配置了RoleVoter来进行投票:
    public int vote(Authentication authentication, Object object, ConfigAttributeDefinition config) {
        int result = ACCESS_ABSTAIN;
        //这里取得资源的安全配置
        Iterator iter = config.getConfigAttributes();

        while (iter.hasNext()) {
            ConfigAttribute attribute = (ConfigAttribute) iter.next();
            
            if (this.supports(attribute)) {
                result = ACCESS_DENIED;

                // 这里对资源配置的安全授权级别进行判断,也就是匹配ROLE为前缀的角色配置
                // 遍历每个配置属性,如果其中一个匹配该主体持有的GrantedAuthority,则访问被允许。
                for (int i = 0; i < authentication.getAuthorities().length; i++) {
                    if (attribute.getAttribute().equals(authentication.getAuthorities()[i].getAuthority())) {
                        return ACCESS_GRANTED;
                    }
                }
            }
        }

        return result;
    }

上面就是对整个授权过程的一个分析,从FilterSecurityInterceptor拦截Http请求入手,然后读取对资源的安全配置以后,把这些信息交由AccessDecisionManager来进行决策,Spring为我们提供了若干决策器来使用,在决策器中我们可以配置投票器来完成投票,我们在上面具体分析了角色投票器的使用过程。
分享到:
评论

相关推荐

    Spring源代码解析(十):Spring_Acegi框架授权的实现.doc

    Spring源代码解析(十):Spring_Acegi框架授权的实现.doc

    Spring源代码解析.rar

    Spring源代码解析1:IOC容器.doc Spring源代码解析2:IoC容器在Web容器中的启动.doc Spring源代码解析3:Spring JDBC .doc Spring源代码解析4:Spring MVC .doc ...Spring源代码解析10:Spring Acegi框架授权的实现.doc

    Spring 源代码解析

    Spring源代码解析1:IOC容器;Spring源代码解析2:IoC容器在Web容器中的启动;Spring源代码解析3:Spring JDBC ;...Spring源代码解析9:Spring Acegi框架鉴权的实现 Spring源代码解析10:Spring Acegi框架授权的实现

    Spring源代码解析

    Spring源代码解析(一):IOC容器 Spring源代码解析(二):IoC容器在Web容器中的启动 Spring源代码解析(三):Spring JDBC Spring源代码解析(四):Spring MVC ...Spring源代码解析(十):Spring Acegi框架授权的实现

    Spring源代码解析(九):Spring_Acegi框架鉴权的实现.doc

    Spring源代码解析(九):Spring_Acegi框架鉴权的实现.doc

    spring源码分析(1-10)

    Spring源代码解析(一):Spring中的事务处理 Spring源代码解析(二):ioc容器在Web容器中的启动 Spring源代码分析(三):Spring JDBC Spring源代码解析(四):Spring ...Spring源代码解析(十):Spring Acegi框架授权的实现

    spring源码分析

    1.Spring源代码解析(一):Spring中的事务处理 2. Spring源代码解析(二):ioc容器在Web容器中的启动 3.Spring源代码解析(三):Spring JDBC ... Spring源代码解析(十):Spring Acegi框架授权的实现

    Spring源码学习文档,绝对值得好好研究~~

    Spring源代码解析(一)Spring中的事务处理.doc Spring源代码解析(二):ioc容器在Web容器中的启动.doc Spring源代码分析(三):Spring JDBC.doc ...Spring源代码解析(十):Spring Acegi框架授权的实现.doc

    Spring技术内幕:深入解析Spring架构与设计原理(第2部分)

     揭开Spring源代码的神秘面纱,展示系统阅读开源软件源代码的方法和秘诀。  如果你正在思考下面这些问题,也许《Spring技术内幕:深入解析Spring架构与设计原理》就是你想要的!  掌握Spring的架构原理与设计思想...

    Spring技术内幕:深入解析Spring架构与设计原理

    《Spring技术内幕:深入解析Spring架构与设计原理(第2版)》从源代码的角度对Spring的内核和各个主要功能模块的架构、设计和实现原理进行了深入剖析。你不仅能从本书中参透Spring框架的出色架构和设计思想,还能从...

    Spring技术内幕:深入解析 Spring架构与设计原理.pdf

    本书从源代码的角度对Spring的内核和各个主要功能模块的架构、设计和实现原理进行了深入剖析。你不仅能从本书中参透Spring框架的优秀架构和设计思想,还能从Spring优雅的实现源码中一窥Java语言的精髓。本书在开篇...

    实战Acegi:使用Acegi作为基于Spring框架的WEB应用的安全框架

    Acegi是一个专门为SpringFramework应用提供安全机制的开放源代码项目,全称为Acegi Security System for Spring,当前版本为 0.8.3。它使用了Spring的方式提供了安全和认证安全服务,包括使用Bean Context,拦截器和...

    Spring技术内幕:深入解析Spring架构与设计原理(第2版)

    Spring技术内幕:深入解析Spring架构与设计原理(第2版)》是国内唯一一本系统分析Spring源代码的著作,也是Spring领域的问鼎之作,由业界拥有10余年开发经验的资深Java专家亲自执笔,Java开发者社区和Spring开发者...

    Spring技术内幕:深入解析Spring架构与设计原理(第2版) 决战大数据时代!IT技术人员不得不读! 计文柯 著

    《Spring技术内幕:深入解析Spring架构与设计原理(第2版)》是国内一本系统分析Spring源代码的著作,也是Spring领域的问鼎之作,由业界拥有10余年开发经验的专业Java专家亲自执笔,Java开发者社区和Spring开发者...

    Spring技术内幕:深入解析Spring架构与设计原理(第1部分)

     揭开Spring源代码的神秘面纱,展示系统阅读开源软件源代码的方法和秘诀。  如果你正在思考下面这些问题,也许《Spring技术内幕:深入解析Spring架构与设计原理》就是你想要的!  掌握Spring的架构原理与设计思想...

    Spring技术内幕:深入解析Spring架构与设计原理(第一部分)

     揭开Spring源代码的神秘面纱,展示系统阅读开源软件源代码的方法和秘诀。  如果你正在思考下面这些问题,也许《Spring技术内幕:深入解析Spring架构与设计原理》就是你想要的!  掌握Spring的架构原理与设计思想...

    Spring技术内幕:深入解析Spring架构与设计原理 1/2

     揭开spring源代码的神秘面纱,展示系统阅读开源软件源代码的方法和秘诀。  掌握spring的架构原理与设计思想真的能让开发者如虎添翼吗?  ioc容器如何掌控以pojo为基础的bean对象?它的轻量级开发设计思想是如何...

    Spring技术内幕:深入解析Spring架构与设计原理(第2版)

    国内唯一一本系统分析Spring源代码的著作,也是Spring领域的问鼎之作,由业界拥有10余年开发经验的资深Java专家亲自执笔,Java开发者社区和Spring开发者社区联袂推荐。 第1版不仅在内容上获得了读者的广泛好评,而且...

Global site tag (gtag.js) - Google Analytics