`
Dead_knight
  • 浏览: 1201067 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
博客专栏
752c8642-b795-3fe6-946e-a4e845bffdec
Spring Securi...
浏览量:240230
33caa84e-18a6-3036-a82b-6e2106a4de63
clojure专题
浏览量:48938
E17ca077-44df-3816-a3fe-471c43f6e1e5
WebLogic11g
浏览量:236878
社区版块
存档分类
最新评论

Spring Security3源码分析-RequestCacheAwareFilter分析

阅读更多
RequestCacheAwareFilter过滤器对应的类路径为
org.springframework.security.web.savedrequest.RequestCacheAwareFilter

这个filter的用途官方解释是
用于用户登录成功后,重新恢复因为登录被打断的请求
这个解释也有几点需要说明
被打算的请求:简单点说就是出现了AuthenticationException、AccessDeniedException两类异常
重新恢复:既然能够恢复,那肯定请求信息被保存到cache中了

首先看被打断请求是如何保存到cache中的
实际上,上一篇的ExceptionTranslationFilter分析已经提到了
requestCache.saveRequest(request, response)
是的,如果出现AuthenticationException或者是匿名登录的抛出了AccessDeniedException,都会把当前request保存到cache中。这里的cache是HttpSessionRequestCache,接着看HttpSessionRequestCache的saveRequest方法
    public void saveRequest(HttpServletRequest request, HttpServletResponse response) {
        //由于构造HttpSessionRequestCache的bean时,没有设置justUseSavedRequestOnGet属性,所以该属性为默认值false。
        if (!justUseSavedRequestOnGet || "GET".equals(request.getMethod())) {
            //构造DefaultSavedRequest,并且设置到session中
            DefaultSavedRequest savedRequest = new DefaultSavedRequest(request, portResolver);

            if (createSessionAllowed || request.getSession(false) != null) {
                request.getSession().setAttribute(DefaultSavedRequest.SPRING_SECURITY_SAVED_REQUEST_KEY, savedRequest);
            }
        }

    }

这里应该知道,实际上被打断的请求被封装成DefaultSavedRequest对象保存到session中了

分析完保存被打断的请求,接着就分析如何恢复被打断的请求了。RequestCacheAwareFilter过滤器就是完成恢复的工作。看doFilter方法
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        //根据当前session取出DefaultSavedRequest,如果有被打断的请求,就把当前请求与被打断请求做匹配。如果匹配成功,对当前请求封装,再传递到下一个过滤器
        HttpServletRequest wrappedSavedRequest =
            requestCache.getMatchingRequest((HttpServletRequest)request, (HttpServletResponse)response);
        chain.doFilter(wrappedSavedRequest == null ? request : wrappedSavedRequest, response);
    }

继续看HttpSessionRequestCache处理过程
    //从当前session中提取DefaultSavedRequest对象
    public SavedRequest getRequest(HttpServletRequest currentRequest, HttpServletResponse response) {
        HttpSession session = currentRequest.getSession(false);

        if (session != null) {
            return (DefaultSavedRequest) session.getAttribute(DefaultSavedRequest.SPRING_SECURITY_SAVED_REQUEST_KEY);
        }

        return null;
    }
    //清除被打断请求
    public void removeRequest(HttpServletRequest currentRequest, HttpServletResponse response) {
        HttpSession session = currentRequest.getSession(false);

        if (session != null) {
            logger.debug("Removing DefaultSavedRequest from session if present");
            session.removeAttribute(DefaultSavedRequest.SPRING_SECURITY_SAVED_REQUEST_KEY);
        }
    }
    //请求匹配
    public HttpServletRequest getMatchingRequest(HttpServletRequest request, HttpServletResponse response) {
        DefaultSavedRequest saved = (DefaultSavedRequest) getRequest(request, response);
        //如果没有被打断请求,直接返回null,不做处理
        if (saved == null) {
            return null;
        }
        //如果当前请求与被打断请求不匹配,直接返回null,不做处理
        if (!saved.doesRequestMatch(request, portResolver)) {
            logger.debug("saved request doesn't match");
            return null;
        }
        //清除被打断请求
        removeRequest(request, response);
        //重新包装当前请求为被打断请求的各项信息
        return new SavedRequestAwareWrapper(saved, request);
    }


接着分析doesRequestMatch方法,看请求是如何匹配的
    //就是比较request与cache中被打断请求的各项信息是否相同
    //这里有个疑惑(由于被打断请求包括POST、GET提交方式的,而这里要求必须为GET方式的请求才会匹配成功)
    public boolean doesRequestMatch(HttpServletRequest request, PortResolver portResolver) {

        if (!propertyEquals("pathInfo", this.pathInfo, request.getPathInfo())) {
            return false;
        }

        if (!propertyEquals("queryString", this.queryString, request.getQueryString())) {
            return false;
        }

        if (!propertyEquals("requestURI", this.requestURI, request.getRequestURI())) {
            return false;
        }

        if (!"GET".equals(request.getMethod()) && "GET".equals(method)) {
            // A save GET should not match an incoming non-GET method
            return false;
        }

        if (!propertyEquals("serverPort", new Integer(this.serverPort), new Integer(portResolver.getServerPort(request))))
        {
            return false;
        }

        if (!propertyEquals("requestURL", this.requestURL, request.getRequestURL().toString())) {
            return false;
        }

        if (!propertyEquals("scheme", this.scheme, request.getScheme())) {
            return false;
        }

        if (!propertyEquals("serverName", this.serverName, request.getServerName())) {
            return false;
        }

        if (!propertyEquals("contextPath", this.contextPath, request.getContextPath())) {
            return false;
        }

        if (!propertyEquals("servletPath", this.servletPath, request.getServletPath())) {
            return false;
        }

        return true;
    }

这里为何对POST请求匹配不成功,目前还不知道具体设计思路。知道后会进行补充
分享到:
评论
1 楼 步青龙 2012-05-09  
多谢楼主共享,辛苦了

相关推荐

Global site tag (gtag.js) - Google Analytics