`
lee79
  • 浏览: 103280 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

springSecurity源码分析——DelegatingFilterProxy类的作用

 
阅读更多

http://www.cnblogs.com/hzhuxin/archive/2011/12/19/2293730.html

 

使用过springSecurity的朋友都知道,首先需要在web.xml进行以下配置,

<filter>
  <filter-name>springSecurityFilterChain</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
 </filter>

<filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>

 </filter-mapping>

从这个配置中,可能会给我们造成一个错觉,以为 DelegatingFilterProxy类就是springSecurity的入口,但其实这个类位于spring-web- 3.0.5.RELEASE.jar这个jar下面,说明这个类本身是和springSecurity无关。DelegatingFilterProxy类继承于抽象类GenericFilterBean,间接地implement 了javax.servlet.Filter接口,Servlet容器在启动时,首先会调用Filter的init方法,GenericFilterBean的作用主要是可以把Filter的初始化参数自动地set到继承于GenericFilterBean类的Filter中去。在其init方法的如下代码就是做了这个事:

1
2
3
4
5
6
PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties);
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader));
initBeanWrapper(bw);
bw.setPropertyValues(pvs, true);

 另外在init方法中调用了initFilterBean()方法,该方法是GenericFilterBean类是特地留给子类扩展用的,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
protected void initFilterBean() throws ServletException {
        // If no target bean name specified, use filter name.
        if (this.targetBeanName == null) {
            this.targetBeanName = getFilterName();
        }
 
        // Fetch Spring root application context and initialize the delegate early,
        // if possible. If the root application context will be started after this
        // filter proxy, we'll have to resort to lazy initialization.
        synchronized (this.delegateMonitor) {
            WebApplicationContext wac = findWebApplicationContext();
            if (wac != null) {
                this.delegate = initDelegate(wac);
            }
        }
    }

 可以看出上述代码首先看Filter是否提供了targetBeanName初始化参数,如果没有提供则直接使用filter的name做为beanName,产生了beanName后,由于我们在web.xml的filter的name是springSecurityFilterChain,从spring的IOC容器中取出bean的代码是initDelegate方法,下面是该方法代码:

1
2
3
4
5
6
7
protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
        Filter delegate = wac.getBean(getTargetBeanName(), Filter.class);
        if (isTargetFilterLifecycle()) {
            delegate.init(getFilterConfig());
        }
        return delegate;
}

 通过跟踪代码,发现取出的bean是org.springframework.security.FilterChainProxy,该类也是继承于GenericFilterBean,取出bean后,判断targetFilterLifecycle属性是false还是true,决定是否调用该类的init方法。这个FilterChainProxy bean实例最终被保存在DelegatingFilterProxy类的delegate属性里,

下面看一下DelegatingFilterProxy类的doFilter方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
 
        // Lazily initialize the delegate if necessary.
        Filter delegateToUse = null;
        synchronized (this.delegateMonitor) {
            if (this.delegate == null) {
                WebApplicationContext wac = findWebApplicationContext();
                if (wac == null) {
                    throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
                }
                this.delegate = initDelegate(wac);
            }
            delegateToUse = this.delegate;
        }
 
        // Let the delegate perform the actual doFilter operation.
        invokeDelegate(delegateToUse, request, response, filterChain);
    }

 真正要关注invokeDelegate(delegateToUse, request, response, filterChain);这句代码,在下面可以看出DelegatingFilterProxy类实际是用其delegate属性即org.springframework.security.FilterChainProxy实例的doFilter方法来响应请求。

1
2
3
4
5
6
protected void invokeDelegate(
            Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
 
        delegate.doFilter(request, response, filterChain);
    }

 

以上就是DelegatingFilterProxy类的一些内部运行机制,其实主要作用就是一个代理模式的应用,可以把servlet 容器中的filter同spring容器中的bean关联起来。

分享到:
评论

相关推荐

    spring security 参考手册中文版

    Spring Security 参考 1 第一部分前言 15 1.入门 16 2.介绍 17 2.1什么是Spring Security? 17 2.2历史 19 2.3版本编号 20 2.4获得Spring安全 21 2.4.1使用Maven 21 Maven仓库 21 Spring框架 22 2.4.2 Gradle 23 ...

    SpringSecurity 3.0.1.RELEASE.CHM

    1.1. Spring Security是什么? 1.2. 历史 1.3. 发行版本号 1.4. 获得Spring Security 1.4.1. 项目模块 1.4.1.1. Core - spring-security-core.jar 1.4.1.2. Web - spring-security-web.jar 1.4.1.3. Config -...

    Spring Security-3.0.1中文官方文档(翻译版)

    Spring Security-3.0.1 中文官方文档(翻译版) 这次发布的Spring Security-3.0.1 是一个bug fix 版,主要是对3.0 中存在的一些问题进 行修 正。文档中没有添加新功能的介绍,但是将之前拼写错误的一些类名进行...

    Spring Security 中文教程.pdf

    1.1. Spring Security是什么? 1.2. 历史 1.3. 发行版本号 1.4. 获得Spring Security 1.4.1. 项目模块 1.4.1.1. Core - spring-security-core.jar 1.4.1.2. Web - spring-security-web.jar 1.4.1.3. ...

    Spring Security3 张卫滨(译)

    添加Spring DelegatingFilterProxy到web.xml文件...................................................... 20 添加Spring Security XML配置文件的应用到web.xml ................................................ ...

    DelegatingFilterProxy示例

    DelegatingFilterProxy代码示例,包含普通filter和代理filter两个示例,帮助加深对DelegatingFilterProxy的理解。

    spring-web-2.5.jar

    org.springframework.web.filter.DelegatingFilterProxy.class org.springframework.web.filter.GenericFilterBean.class org.springframework.web.filter.Log4jNestedDiagnosticContextFilter.class org.spring...

    SSH集成代理2.0版和struts.xml中DelegatingActionProxy代理搭配

    SSH集成代理2.0版和struts.xml中DelegatingActionProxy代理搭配

    SPRING API 2.0.CHM

    DelegatingFilterProxy DelegatingIntroductionInterceptor DelegatingJob DelegatingMessageSource DelegatingNavigationHandlerProxy DelegatingPhaseListenerMulticaster DelegatingRequestProcessor ...

    简单配置 shiro + spring +springMVC+hibernate简单框架

    org.springframework.web.filter.DelegatingFilterProxy &lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;shiroFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; maven...

    尚硅谷Shiro视频教程

    尚硅谷_Shiro_源码、课件 · 01.尚硅谷_Shiro_简介 · 02.尚硅谷_Shiro_HelloWorld · 03.尚硅谷_Shiro_集成 Spring · 04.尚硅谷_Shiro_工作流程(1) · 05.尚硅谷_Shiro_DelegatingFilterProxy · 06. 尚硅谷...

    gradle-spring-4-mvc-boilerplate

    如该软件包所指定的那样,DelegatingFilterProxy被认为可与Spring Web MVC一起使用,并且仅与Spring Web MVC一起使用(aka控制器,带或不带注释)。 它似乎不能与普通的servlet-jsps一起使用,因为您似乎正在尝试...

    Nginx安装包

    4 &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy 5 6 7 &lt;filter-name&gt;springSessionRepositoryFilter 8 &lt;url-pattern&gt;/* 9 完成这一步以后,我们对redis的集成就完成了,接下来我们测试...

    单点登录sso-shiro-cas-maven

    class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"&gt; &lt;bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"&gt; ...

    inject-dependencies-into-your-filters

    Dead Code Rising 代码示例展示了如何使用 Spring 的 DelegatingFilterProxy 将依赖项注入过滤器。 在阅读文章。

Global site tag (gtag.js) - Google Analytics