Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter order in spring-boot

How can I specify order of my Filter in spring-boot? I need to insert my MDC filter after Spring Security filter. I tried almost everything but my filter was always first. This didn't work:

@Bean @Order(Ordered.LOWEST_PRECEDENCE) public UserInsertingMdcFilter userInsertingMdcFilter() {     return new UserInsertingMdcFilter(); } 

This didn't work too:

@Bean public FilterRegistrationBean userInsertingMdcFilterRegistrationBean() {     FilterRegistrationBean registrationBean = new FilterRegistrationBean();     UserInsertingMdcFilter userFilter = new UserInsertingMdcFilter();     registrationBean.setFilter(userFilter);     registrationBean.setOrder(Integer.MAX_VALUE);     return registrationBean; } 
like image 275
igo Avatar asked Sep 21 '14 09:09

igo


People also ask

What is security filter chain in Spring boot?

Spring Security maintains a filter chain internally where each of the filters has a particular responsibility and filters are added or removed from the configuration depending on which services are required. The ordering of the filters is important as there are dependencies between them.

How does filter work in Spring boot?

In Spring boot, we have filters to filter the HTTP request; filter, in general, is used to intercept the request, i.e. HTTP request and the response from the client-side. By the use of a filter, we can perform two operations which can be done on response and request.

In what way can we add filter to a Spring boot application?

There are three ways to add your filter, Annotate your filter with one of the Spring stereotypes such as @Component. Register a @Bean with Filter type in Spring @Configuration. Register a @Bean with FilterRegistrationBean type in Spring @Configuration.


2 Answers

Guys from Spring helped again. See https://github.com/spring-projects/spring-boot/issues/1640 and https://jira.spring.io/browse/SEC-2730

Spring Security doesn't set an order on the Filter bean that it creates. This means that, when Boot is creating a FilterRegistrationBean for it, it gets the default order which is LOWEST_PRECEDENCE.

If you want your own Filter to go after Spring Security's you can create your own registration for Spring Security's filter and specify the order.

So the answer to my question is:

@Bean public FilterRegistrationBean securityFilterChain(@Qualifier(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME) Filter securityFilter) {     FilterRegistrationBean registration = new FilterRegistrationBean(securityFilter);     registration.setOrder(Integer.MAX_VALUE - 1);     registration.setName(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME);     return registration; }  @Bean public FilterRegistrationBean userInsertingMdcFilterRegistrationBean() {     FilterRegistrationBean registrationBean = new FilterRegistrationBean();     UserInsertingMdcFilter userFilter = new UserInsertingMdcFilter();     registrationBean.setFilter(userFilter);     registrationBean.setOrder(Integer.MAX_VALUE);     return registrationBean; } 
like image 199
igo Avatar answered Sep 18 '22 23:09

igo


This was fixed in Spring Boot 1.2. The security chain now defaults to order 0.

It can also be set via properties:

security.filter-order=0 # Security filter chain order. 

https://github.com/spring-projects/spring-boot/issues/1640

like image 28
Mikhail Avatar answered Sep 16 '22 23:09

Mikhail