'How to change the order of all the servlet filters
There is a package I am importing. In that package I have a bean FilterRegistrationBean making it the lowest precedence. I want to make it a certain precedence. Let's say the lowest(MAX_INT). When I import the package I found out that the current filter in the project already takes the lowest precedence(MAX_INT). With two filters being the lowest precedence, when the request comes the imported Filter always goes before the filter in the current project. Is there a way to see all the ordering of the filters and reorder them by changing some code on the importing package. I do not want to change my current project code.
This is the imported package code.
@Bean
@Order(Ordered.LOWEST_PRECEDENCE)
public LoggingFilter loggingFilter() {
return new LoggingFilter();
}
@Bean
public FilterRegistrationBean loggingFilterRegistrationBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
LoggingFilter loggingFilter = loggingFilter();
registrationBean.setFilter(loggingFilter);
registrationBean.setOrder(Integer.MAX_VALUE);
return registrationBean;
}
@Component
@Slf4j
public class LoggingFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
"logs the request"
}
}
This is the filter in the current project
@Component
@Slf4j
public class UrlFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
"filter request base on some url"
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
