有办法禁用嵌入式Servlet过滤器吗?
我的项目有一个依赖jar,它包含(在jar中)映射到@WebFilter的"/*"。
我需要jar (它有我公司的很多公用类),但是这个新项目不需要这个WebFilter,实际上这个新项目不能工作,因为这个过滤器检查用户身份验证,而新项目没有"loggedUser“。就像一个网站
谢谢
发布于 2013-09-09 19:00:50
正是出于这个原因,web.xml优先于注释。只需将web.xml中的违规过滤器声明为好的旧方法,并将其<filter-mapping>设置为假的(如:
<filter>
<filter-name>BadBadFilter</filter-name>
<filter-class>com.example.BadBadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>BadBadFilter</filter-name>
<url-pattern>/this-path-does-not-exist/*</url-pattern>
</filter-mapping>这将有效地禁用它。
发布于 2018-10-04 01:52:46
如果您正在使用Spring,@WebFilter可以由Server自动实例化,而无需依赖Beans定义。解决这个问题的方法是在嵌入式Tomcat标识@WebFilter之前,在Spring中注册我自己的过滤器。这样,@WebFilter之前就已经注册了,嵌入式服务器不会覆盖您的服务器。
为了实现这一点,您需要在服务器找到过滤器之前注册它。我确实注册了我的过滤器,并做了如下更改:
/**
* Creates the Bean. Observe that @WebFilter is not registered as a Bean.
*/
@Bean
public SomeFilter someFilter() {
return new SomeFilter();
}其次,您需要使用相同的名称注册。重要的是找到服务器用于注册过滤器的名称。通常,如果@WebFilter标记没有提供,它将是类的完整名称
/**
* It is important to keep the same name; when Apache Catalina tries to automatically register the filter,
* it will check that is has already been registered.
* @param filter SomeFilter
* @return
*/
@Bean
public FilterRegistrationBean registration(SomeFilter filter) {
FilterRegistrationBean registration = new FilterRegistrationBean(filter);
registration.setEnabled(true);
registration.setAsyncSupported(true);
registration.setName("some.class.path.for.some.filter.SomeFilter");
registration.setUrlPatterns(Lists.<String>newArrayList("/some-url-does-not-exist/*"));
return registration;
}在我的场景中,我必须启用async=true,所以我也添加了这一行。
https://stackoverflow.com/questions/18704226
复制相似问题