I am trying to set the order of execution of 2 filters in my spring boot application which have same url mapping. I have tried using 2 filter registration beans in my main A
setOrder(int) method does the job.
below is an example
@Configuration
@EnableAutoConfiguration
@EnableWebMvc
@ComponentScan
public class Application {
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
SecurityFilter securityFilter = new SecurityFilter();
registrationBean.setFilter(securityFilter);
registrationBean.setOrder(2);
return registrationBean;
}
@Bean
public FilterRegistrationBean contextFilterRegistrationBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
RequestContextFilter contextFilter = new RequestContextFilter();
registrationBean.setFilter(contextFilter);
registrationBean.setOrder(1);
return registrationBean;
}
}
Spring Boot sorts your FilterRegistrationBean
using AnnotationAwareOrderComparator
before applying them to the servlet context. The RegistrationBean
is not currently Ordered
so there is no way to set the order by calling a method, but you can work around that by creating subclasses and adding @Order
to them. I think making the base class Ordered
and providing a setter is probably a useful thing to do in the framework (open an issue on github if you agree).
Update: Ordered
was added in 1.0.x.
Bean name will solve your problem: @Bean("aFilter").