DelegatingFilterProxy类的作用

时间:2023-03-08 17:40:38
DelegatingFilterProxy类的作用

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

  1. <filter>
  2. <filter-name>springSecurityFilterChain</filter-name>
  3. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  4. <init-param>
  5. <param-name>targetFilterLifecycle</param-name>
  6. <param-value>true</param-value>  <!-- 默认是false -->
  7. </init-param>
  8. </filter>
  9. <filter-mapping>
  10. <filter-name>springSecurityFilterChain</filter-name>
  11. <url-pattern>/*</url-pattern>
  12. </filter-mapping>

非springSecurity用法如下:

  1. <filter>
  2. <filter-name>DelegatingFilterProxy</filter-name>
  3. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  4. <init-param>
  5. <param-name>targetBeanName</param-name>
  6. <!-- 自定义filter -->
  7. <param-value>exportExamineFilter</param-value>
  8. </init-param>
  9. <init-param>
  10. <!-- 判断targetFilterLifecycle属性是false还是true,决定是否调用自定义类的init()、destry()方法 -->
  11. <param-name>targetFilterLifecycle</param-name>
  12. <param-value>false</param-value>
  13. </init-param>
  14. </filter>
  15. <filter-mapping>
  16. <filter-name>DelegatingFilterProxy</filter-name>
  17. <url-pattern>/*</url-pattern>
  18. </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. PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties);
  2. BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
  3. ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
  4. bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader));
  5. initBeanWrapper(bw);
  6. bw.setPropertyValues(pvs, true);

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

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

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

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

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

下面看一下DelegatingFilterProxy类的doFilter方法

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

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

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

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

此外还要注意一个DelegatingFilterProxy的一个初始化参数:targetFilterLifecycle ,其默认值为false 。 但如果被其代理的filter的init()方法和destry()方法需要被调用时,需要设置targetFilterLifecycle为true。具体可见DelegatingFilterProxy中的如下代码:

  1. protected void initFilterBean() throws ServletException {
  2. synchronized (this.delegateMonitor) {
  3. if (this.delegate == null) {
  4. // If no target bean name specified, use filter name.
  5. if (this.targetBeanName == null) {
  6. this.targetBeanName = getFilterName();
  7. }
  8. // Fetch Spring root application context and initialize the delegate early,
  9. // if possible. If the root application context will be started after this
  10. // filter proxy, we'll have to resort to lazy initialization.
  11. WebApplicationContext wac = findWebApplicationContext();
  12. if (wac != null) {
  13. this.delegate = initDelegate(wac);
  14. }
  15. }
  16. }
  17. }
  18. protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
  19. Filter delegate = wac.getBean(getTargetBeanName(), Filter.class);
  20. if (isTargetFilterLifecycle()) {    //注意这行
  21. delegate.init(getFilterConfig());
  22. }
  23. return delegate;
  24. }