Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define Servlet filter order of execution in Spring Boot application

Tags:

spring-boot

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 Application class as below but that did not work. I want the authorizationFilter to be hit first then the validationFilter. But it is always hitting ONLY validationFilter when both are configured. If I comment out the validationFilter, it hits authorizationFilter.

@Bean public FilterRegistrationBean authorizationFilter(){     FilterRegistrationBean filterRegBean = new FilterRegistrationBean();     filterRegBean.setFilter(authorizationFilter);     List<String> urlPatterns = new ArrayList<String>();     urlPatterns.add("/v1/*");     filterRegBean.setUrlPatterns(urlPatterns);     return filterRegBean; }  @Bean public FilterRegistrationBean validationFilter(){     FilterRegistrationBean filterRegBean = new FilterRegistrationBean();     filterRegBean.setFilter(validationFilter);     List<String> urlPatterns = new ArrayList<String>();     urlPatterns.add("/v1/*");     filterRegBean.setUrlPatterns(urlPatterns);     return filterRegBean; } 

I have also tried introducing web.xml and converting the executable jar to war file.

<web-app>    <filter>     <filter-name>authorizationFilter</filter-name>     <filter-class>com.security.filter.AuthorizationFilter</filter-class> </filter> <filter-mapping>     <filter-name>authorizationFilter</filter-name>     <url-pattern>/v1/*</url-pattern> </filter-mapping>  <filter>     <filter-name>validationFilter</filter-name>     <filter-class>com.security.validation.ValidationFilter</filter-class> </filter> <filter-mapping>     <filter-name>validationFilter</filter-name>     <url-pattern>/v1/*</url-pattern> </filter-mapping> </web-app> 

But the application doesn't seem to recognize the web.xml, as it hits only the validation filter with the configuration above. I appreciate any inputs in resolving this. Thanks

like image 358
Manu Avatar asked Mar 17 '14 11:03

Manu


People also ask

What is Servlet Filter Spring Boot?

Spring Boot - Servlet Filter. A filter is an object used to intercept the HTTP requests and responses of your application. By using filter, we can perform two operations at two instances −. Before sending a response to the client.

How to create filters in Spring Boot?

Let's start by creating two filters: In order to create a filter, we simply need to implement the Filter interface: In order for Spring to recognize a filter, we need to define it as a bean with the @Component annotation. Moreover, to have the filters fire in the right order, we need to use the @Order annotation.

How to implement Servlet Filters?

The Servlet Filter implementation is really simple, we implement the javax.servlet.Filter, the @Order specifies the order in which the filters have to be executed. We have specified the order as 1 so this will be the first filter to be executed.

What is the default order of a filterregistrationbean?

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.


1 Answers

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;     } } 
like image 95
Ioseb Khutsishvili Avatar answered Sep 19 '22 09:09

Ioseb Khutsishvili