I'm trying to insert (at first position) a simple custom Cors filter inside the spring filter chain.
If I do it like this
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {
it works perfectly I can verify it by putting a breakpoint in Spring's ServletHandler.java where there the line
chain=getFilterChain(baseRequest, target, servlet_holder);
I was just wondering if I wish not to use @Componenent and @Order
and instead defining the Filter bean the Application context.
How can I set the order of the filters ?
Another alternative is to have your filter implement
org.springframework.core.Ordered
, and this is the only option if you're using spring webflux (it doesn't have aFilterRegistrationBean
):or you can even make it a variable in your filter.
In case of corsFilter, it is normally required to set the order of this filter to be loaded before springSecurityFilterChain and other filters such as errorPageFilter from spring boot to be laded at the very beginning of the chain. Otherwise CORS support will be broken and the required headers won't be introduced. This can be accomplished in following matter:
The bean.setOrder(Integer.MIN_VALUE) will make sure that is is the very first filter loaded. Even if there is other filters such as errorPage filter with order set to Integer.MIN_VALUE(-2147483648).
See example: In your class ServletInitializer:
the name of my filter is "requestLogFilter"
Warning: Don't use @Component annotation at the class Filter.