Spring DelegatingFilterProxy执行顺序问题

时间:2015-09-24 17:19:36

标签: java spring spring-mvc servlet-filters

我按照以下方式定义了web.xml

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

  <filter-mapping>
   <filter-name>Filter1</filter-name>
   <url-pattern>*.do</url-pattern>
  </filter-mapping>



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

  <filter-mapping>
   <filter-name>Filter2</filter-name>
   <url-pattern>/filter2.do</url-pattern>
  </filter-mapping>


  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:filter1-beans-def.xml
            classpath:filter2-bean-def.xml
        </param-value>
    </context-param>

      //dispatcher Servlet defination

从上面的序列filter1首先定义,然后是第二个过滤器。在上下文中定义其各自bean的情况也是如此。

但实际上我的过滤器按此顺序执行:过滤器2 ---&gt;过滤1 。 我想以相反的顺序执行它们吗?

这里发生了什么问题?谢谢

1 个答案:

答案 0 :(得分:0)

根据我读过的所有文档,过滤器应按照定义过滤器映射元素的顺序执行。

How do web.xml filters work? Can you have two filters map to all pages (/*) and specify the order?

  

调用过滤器的顺序是其中的顺序   与servlet的请求URI匹配的filter-mapping声明   出现在过滤器映射元素列表

http://docs.oracle.com/javaee/6/tutorial/doc/bnagb.html

  

链中过滤器的顺序与顺序相同   哪些筛选器映射出现在Web应用程序部署中   描述符。

要尝试的一件事是重新排列过滤器和过滤器映射元素以组合在一起:

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

<filter-mapping>
   <filter-name>Filter1</filter-name>
   <url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
   <filter-name>Filter2</filter-name>
   <url-pattern>/filter2.do</url-pattern>
</filter-mapping>