How can I add a filter class in Spring Boot? How can I add a filter class in Spring Boot? java java

How can I add a filter class in Spring Boot?


If you want to setup a third-party filter you can use FilterRegistrationBean.

For example, the equivalent of web.xml:

<filter>     <filter-name>SomeFilter</filter-name>        <filter-class>com.somecompany.SomeFilter</filter-class></filter><filter-mapping>    <filter-name>SomeFilter</filter-name>    <url-pattern>/url/*</url-pattern>    <init-param>        <param-name>paramName</param-name>        <param-value>paramValue</param-value>    </init-param></filter-mapping>

These will be the two beans in your @Configuration file:

@Beanpublic FilterRegistrationBean someFilterRegistration() {    FilterRegistrationBean registration = new FilterRegistrationBean();    registration.setFilter(someFilter());    registration.addUrlPatterns("/url/*");    registration.addInitParameter("paramName", "paramValue");    registration.setName("someFilter");    registration.setOrder(1);    return registration;}public Filter someFilter() {    return new SomeFilter();}

The above was tested with Spring Boot 1.2.3.


Here is an example of one method of including a custom filter in a Spring Boot MVC application. Be sure to include the package in a component scan:

package com.dearheart.gtsc.filters;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletResponse;import org.springframework.context.annotation.Profile;import org.springframework.stereotype.Component;@Componentpublic class XClacksOverhead implements Filter {  public static final String X_CLACKS_OVERHEAD = "X-Clacks-Overhead";  @Override  public void doFilter(ServletRequest req, ServletResponse res,      FilterChain chain) throws IOException, ServletException {    HttpServletResponse response = (HttpServletResponse) res;    response.setHeader(X_CLACKS_OVERHEAD, "GNU Terry Pratchett");    chain.doFilter(req, res);  }  @Override  public void destroy() {}  @Override  public void init(FilterConfig arg0) throws ServletException {}}


There are three ways to add your filter,

  1. Annotate your filter with one of the Spring stereotypes such as @Component
  2. Register a @Bean with Filter type in Spring @Configuration
  3. Register a @Bean with FilterRegistrationBean type in Spring @Configuration

Either #1 or #2 will do if you want your filter applies to all requests without customization, use #3 otherwise. You don't need to specify component scan for #1 to work as long as you place your filter class in the same or sub-package of your SpringApplication class. For #3, use along with #2 is only necessary when you want Spring to manage your filter class such as have it auto wired dependencies. It works just fine for me to new my filter which doesn't need any dependency autowiring/injection.

Although combining #2 and #3 works fine, I was surprised it doesn't end up with two filters applying twice. My guess is that Spring combines the two beans as one when it calls the same method to create both of them. In case you want to use #3 alone with authowiring, you can AutowireCapableBeanFactory. The following is an example,

private @Autowired AutowireCapableBeanFactory beanFactory;    @Bean    public FilterRegistrationBean myFilter() {        FilterRegistrationBean registration = new FilterRegistrationBean();        Filter myFilter = new MyFilter();        beanFactory.autowireBean(myFilter);        registration.setFilter(myFilter);        registration.addUrlPatterns("/myfilterpath/*");        return registration;    }