Here's an answer compatible with Spring Boot
2 / Spring Security
5 that will allow you to insert your filter in an arbitrary place in the filter chain.
My use case was a custom logging javax.servlet.Filter
that I wanted to execute before any Spring Security
filters; however the below steps should allow you to put a filter anywhere in your existing Spring filter chain:
Step 1: Find out the order of Spring filters in your existing setup.
Connect your favorite remote debugger to your application, and set a breakpoint in the doFilter(ServletRequest request, ServletResponse response)
method of org.springframework.security.web.FilterChainProxy
.
As of Spring Security
5.1.6, that is line 311. In your debugger, find out the existing filters by inspecting this.additionalFilters
. In my application, the order was something like:
0: WebAsyncManagerIntegrationFilter
1: SecurityContextPersistenceFilter
2: HeaderWriterFilter
...
Step 2: Insert your filter in the desired place using Spring's WebSecurityConfigurerAdapter and HttpSecurity
You likely already have a WebSecurityConfigurerAdapter
with a @Override configure(HttpSecurity http)
method. HttpSecurity
exposes addFilterBefore
and addFilterAfter
methods to allow you to place your filter relative to an existing class in the chain. Your filter (instance) is the first argument of these methods, and the class of the filter you'd like to insert before or after is the second argument.
In my case, I wanted my custom logging filter to be first in the chain (my code snippet is Kotlin, I'll leave the Java implementation to you):
override fun configure(http: HttpSecurity) {
http
.addFilterBefore(MyCustomLoggingFilter(), WebAsyncManagerIntegrationFilter::class.java)
.authorizeRequests()
.antMatchers(
...
)
}
Step 3: Profit!
Use the debugging method described in Step 1 above to verify that your filter is where you intended in the filter chain.
Hope this helps someone else out there.
@Order(Ordered.LOWEST_PRECEDENCE + 100)
will not work becauseOrdered.LOWEST_PRECEDENCE = Integer.Max
and Integer.Max + 100 = some negative number, this would mean a very high precedence – Sunday@Order(Ordered.LOWEST_PRECEDENCE)
with no success. – Norean