2018-03-09 Addendum It was the same behavior when I upgraded to Spring Boot 2.0.0.RELEASE.
2018-12-20 postscript Added the explanation of Spring Boot 2.1.
Use the FilterRegistrationBean
class provided by Spring Boot.
When you define a FilterRegistrationBean
, the Filter is fetched in the Spring Boot Auto Configuration class and registered with the Embedded Server.
@Configuration
public class SomeConfig {
@Bean
public FilterRegistrationBean hogeFilter() {
//New Filter and pass it to the FilterRegistrationBean constructor
FilterRegistrationBean bean = new FilterRegistrationBean(new HogeFilter());
//Filter url-Specify pattern (variadic argument allows multiple specifications)
bean.addUrlPatterns("/*");
//Filter execution order. Performed for aiming at integer values
bean.setOrder(Integer.MIN_VALUE);
return bean;
}
@Bean
public FilterRegistrationBean fugaFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean(new FugaFilter());
bean.addUrlPatterns("/*");
//Executed after HogeFilter
bean.setOrder(Integer.MIN_VALUE + 1);
return bean;
}
//Any number of Filters can be defined
}
The @Bean
method can accept other beans as arguments. This is a feature that Spring originally has.
It is OK if the Bean received as an argument is received by the constructor of Filter.
public class HogeFilter extends OncePerRequestFilter { //Of course, "implements Filter" is also OK
private final FugaBean fugaBean; //Create field
public HogeFilter(FugaBean fugaBean) { //Receive the bean in the constructor
this.fugaBean = fugaBean;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
//Write pre-processing here
chain.doFilter(request, response);
//Write post-processing here
}
}
@Configuration
public class HogeConfig {
@Bean
public FilterRegistrationBean hogeFilter(FugaBean fugaBean) {
//Pass the bean to the Filter constructor
FilterRegistrationBean bean = new FilterRegistrationBean(new HogeFilter(fugaBean));
// ...
return bean;
}
}
The FugaBean must be bean-defined somewhere else (such as another Config class or component scan).
Note that the Filter itself is not a bean. That is, you cannot DI a bean to a Filter with
@ Autowired
or apply an AOP to a Filter.
It is a class called ServletContextInitializerBeans
that extracts Filter from FilterRegistrationBean
and registers it in Embedded Server. It is OK if you output the DEBUG log of this class (Note: TRACE log after Spring Boot 2.1).
Filter is executed in ascending order of the value of ʻorder`.
application.properties
# Boot 2.After 1 is trace instead of debug
logging.level.org.springframework.boot.web.servlet.ServletContextInitializerBeans=debug
Standard output
...
2018-02-28 11:58:37.611 DEBUG 4323 --- [ost-startStop-1] o.s.b.w.s.ServletContextInitializerBeans : Added existing Filter initializer bean 'loggingFilter1'; order=-2147483648, resource=com.example.Application
2018-02-28 11:58:37.611 DEBUG 4323 --- [ost-startStop-1] o.s.b.w.s.ServletContextInitializerBeans : Added existing Filter initializer bean 'loggingFilter2'; order=-2147483647, resource=com.example.Application
2018-02-28 11:58:37.612 DEBUG 4323 --- [ost-startStop-1] o.s.b.w.s.ServletContextInitializerBeans : Added existing Filter initializer bean 'loggingFilter3'; order=-2147483646, resource=com.example.Application
2018-02-28 11:58:37.612 DEBUG 4323 --- [ost-startStop-1] o.s.b.w.s.ServletContextInitializerBeans : Added existing Servlet initializer bean 'dispatcherServletRegistration'; order=2147483647, resource=class path resource [org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration.class]
2018-02-28 11:58:37.638 DEBUG 4323 --- [ost-startStop-1] o.s.b.w.s.ServletContextInitializerBeans : Created Filter initializer for bean 'characterEncodingFilter'; order=-2147483648, resource=class path resource [org/springframework/boot/autoconfigure/web/HttpEncodingAutoConfiguration.class]
2018-02-28 11:58:37.638 DEBUG 4323 --- [ost-startStop-1] o.s.b.w.s.ServletContextInitializerBeans : Created Filter initializer for bean 'hiddenHttpMethodFilter'; order=-10000, resource=class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.class]
2018-02-28 11:58:37.638 DEBUG 4323 --- [ost-startStop-1] o.s.b.w.s.ServletContextInitializerBeans : Created Filter initializer for bean 'httpPutFormContentFilter'; order=-9900, resource=class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.class]
2018-02-28 11:58:37.638 DEBUG 4323 --- [ost-startStop-1] o.s.b.w.s.ServletContextInitializerBeans : Created Filter initializer for bean 'requestContextFilter'; order=-105, resource=class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]
...
It is OK to define the Filter itself as a bean, but I cannot specify the url-pattern or execution order. Even if you specify the @ Order
annotation in the @ Bean
method, it seems to be ignored (regardless of the value of @ Order
, the value of order output to the log will be 2147483647
).
@Bean
@Order(Integer.MIN_VALUE) //This value seems to be ignored (not sure if it's a spec)
public HogeFilter hogeFilter() {
return new HogeFilter();
}
So let's use FilterRegistrationBean
.
Spring Boot Reference https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-embedded-container-servlets-filters-listeners-beans Javadoc https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/web/servlet/FilterRegistrationBean.html
Recommended Posts