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
question from:
https://stackoverflow.com/questions/22453707/how-to-define-servlet-filter-order-of-execution-in-spring-boot-application 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…