5

Is there a way to disable a embedded Servlet Filter?

My project has a dependency jar that contains (inside the jar) a @WebFilter mapped to "/*".
I need the jar (it has a lot of commons class of my company), but this new project does not need this WebFilter, actually this new project will not work because this Filter checks user authentication and the new project has no "loggedUser". It's like a website

Thanks

ethanxyz_0
  • 713
  • 12
  • 37

2 Answers2

10

web.xml takes precedence over annotations for precisely this reason. Simply declare the offending filter in web.xml the good old way and set its <filter-mapping> to something bogus, eg:

<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>

This will effectively disable it.

rdcrng
  • 3,415
  • 2
  • 18
  • 36
  • 3
    It seems to me that the `filter-name` in web.xml must match the `filterName` in the `@WebFilter` annotation to perform a valid override. Otherwise the filter declaration in web.xml will be an addition, not an override. – mthmulders May 24 '16 at 07:16
  • This does not stop the filter from initializing. My problem is an error occurs during the embedded filter do init(). How to disable it completely while not removing the dependency? – fwonce Apr 25 '18 at 09:23
  • @fwonce You should take the component library filter and extend it in order to override the init() method – ethanxyz_0 Apr 26 '18 at 08:12
0

If you are using Spring Boot, @WebFilter can get automatically instantiated by the Spring Boot Server without relying on Beans definition. The way I found to solve the issue is to register my own filter in the Spring Boot before the @WebFilter is identified by the embedded Tomcat Server. On that way, the @WebFilter is already registered before and the embedded server does not overwrite yours.

In order to achieve that, you need to register the filter before it is found by the server. I did register my filter and made the changes as follows:

/**
 * Creates the Bean. Observe that @WebFilter is not registered as a Bean.
 */
@Bean
public SomeFilter someFilter() {
    return new SomeFilter();
}

Secondly, you need to register with the same name. It is important to find the name the server is using to register your filter. Usually, if not provided by the @WebFilter tag, it is going to be the complete name of your class

/**
 * 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;
}

In my scenario, I had to enable async=true, so I have added that line as well.