WEB-INF/WEB.xml和conf/WEB.xml中定义的servlet过滤器的实际执行顺序是什么

WEB-INF/WEB.xml和conf/WEB.xml中定义的servlet过滤器的实际执行顺序是什么,web,servlet-filters,Web,Servlet Filters,根据我的理解,过滤器的执行顺序是在web.xml中为相同的url映射定义的顺序。但是,如果我们为容器的web.xml和单个应用程序的web.xml上的同一url映射定义了多个过滤器,那么我找不到任何关于这种行为的参考 我的假设是,由于应用程序是作为容器(比如tomcat)的一部分部署的,它有自己的web.xml,因此针对每个部署的应用程序的任何请求都必须先通过tomcat/conf/web.xml中定义的过滤器链,然后再通过应用程序过滤器。但我的理解是错误的 我有一个简单的web应用程序,在we

根据我的理解,过滤器的执行顺序是在web.xml中为相同的url映射定义的顺序。但是,如果我们为容器的web.xml和单个应用程序的web.xml上的同一url映射定义了多个过滤器,那么我找不到任何关于这种行为的参考

我的假设是,由于应用程序是作为容器(比如tomcat)的一部分部署的,它有自己的web.xml,因此针对每个部署的应用程序的任何请求都必须先通过tomcat/conf/web.xml中定义的过滤器链,然后再通过应用程序过滤器。但我的理解是错误的

我有一个简单的web应用程序,在web.xml中定义了两个过滤器,如下所示

<filter>
    <filter-name>AppFilterOne</filter-name>
    <filter-class>com.test.filters.AppFilterOne</filter-class>
</filter>
<filter>
     <filter-name>AppFilterTwo</filter-name>
     <filter-class>com.test.filters.AppFilterTwo</filter-class>
</filter>
<filter-mapping>
     <filter-name>AppFilterOne</filter-name>
     <url-pattern>/*</url-pattern>
</filter-mapping>

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

AppFilterOne
com.test.filters.AppFilterOne
AppFilterTwo
com.test.filters.AppFilterTwo
AppFilterOne
/*
AppFilterTwo
/*
没有任何进一步的更改,如果我点击我的web url,我会看到AppFilterOne和AppFilterTwo按该顺序被点击

现在,我在tomcat/conf/web.xml中又添加了两个过滤器(我的tomcat版本是7.0.27,支持servlet 3.0)


TomWebFilterOne
com.test.filter.TomWebFilterOne
TomWebFilterTwo
com.test.filter.TomWebFilterTwo
TomWebFilterOne
/*
TomWebFilterTwo
/*
现在,如果我访问,过滤器将按以下顺序执行 AppFilterOne AppFilterTwo TomWebFilterOne TomWebFilterTwo

我最初的假设是,TomWebFilters将首先拦截,然后拦截特定于应用程序的过滤器

如果我看到的结果实际上是正确的,并且过滤器就是这样工作的,那么有没有一种方法可以影响过滤器的执行顺序。我听说了,但不确定这是否是正确的方法

一点背景我的实际问题: 在tomcat 7X实例中,我们在不同的vm中部署了一组web应用程序。每个web应用程序都有一个审计过滤器,用于审计和记录每个传入的请求。但是,在tomcat/conf/web.xml中定义了一个过滤器来支持NTLM身份验证(JCIFS风格)。由于此设置,传入的每个请求实际上都会被记录(作为审核筛选器的一部分),然后针对NTLM进行筛选。我们希望NTLM先发生,然后再发生其他事情

我在这里想,有两种方法 A) 我们可能需要将该过滤器定义为每个应用程序中的第一个过滤器,而不是在tomcat/conf/web.xml中定义。 B) 让NTLM筛选器在请求中设置一个属性,说明NTLM进程的状态,我们的审核筛选器将对此检查两次(即两个401 HTTP状态代码),然后返回

我对这两种方法都不是特别满意,因此想知道可以做些什么


感谢

修复了我最初处理审核日志的问题,我没有在application web.xml中使用过滤器,而是为此使用了AOP。它似乎工作得很好。但我仍然很想知道,是否有一种标准方法可以影响WEB-INF/WEB.xml和conf/WEB.xml之间声明的过滤器序列

<filter>
    <filter-name>TomWebFilterOne</filter-name>
    <filter-class>com.test.filter.TomWebFilterOne</filter-class>
</filter>

 <filter>
    <filter-name>TomWebFilterTwo</filter-name>
        <filter-class>com.test.filter.TomWebFilterTwo</filter-class>
  </filter>

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

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