Quick revision
The short answer
Spring Security is registered with the servlet container as a single filter, a DelegatingFilterProxy, at order -100 by default, and it runs its own SecurityFilterChain inside; our custom filter, registered as a @Component with no order, gets the lowest precedence and so runs after the whole security chain, when the authorization decision has already been made without the user it was supposed to set up. Servlet filter order in Boot comes from @Order, Ordered or FilterRegistrationBean.setOrder, but an authentication filter does not belong in that list at all: it goes inside the security chain with addFilterBefore or addFilterAfter, and its automatic servlet registration should be disabled so it does not also run as a separate filter.
What to remember
In this answer, we saw that Spring Security is a single container filter at order -100 that contains its own SecurityFilterChain, that container filter order comes from @Order, Ordered or FilterRegistrationBean.setOrder, and that a @Component filter with no order runs after security, while one ordered before it has its user overwritten by SecurityContextHolderFilter. We fixed it by adding the filter inside the security chain with addFilterBefore, and prevented a second container registration by not making it a bean or by disabling its FilterRegistrationBean.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Spring Security is registered with the servlet container as a single filter, a DelegatingFilterProxy, at order -100 by default, and it runs its own SecurityFilterChain inside; our custom filter, registered as a @Component with no order, gets the lowest precedence and so runs after the whole security chain, when the authorization decision has already been made without the user it was supposed to set up. Servlet filter order in Boot comes from @Order, Ordered or FilterRegistrationBean.setOrder, but an authentication filter does not belong in that list at all: it goes inside the security chain with addFilterBefore or addFilterAfter, and its automatic servlet registration should be disabled so it does not also run as a separate filter.
Why this question is asked
Writing a custom authentication filter, for an API key or a token from a gateway, is common, and getting it to run at the right moment is where most people stumble. The filter works in a unit test, yet in the application the user is anonymous or the request is rejected before the filter ever sees it. Interviewers ask this debugging question because the answer requires knowing that there are two filter chains, the container's and Spring Security's, and how each is ordered. In this answer, we will recap the two chains, reproduce the problem, see how order is decided, move the filter into the security chain, and prevent it from running twice.
1A Quick Recap: Two Filter Chains
The servlet container has a filter chain: an ordered list of filters it runs before DispatcherServlet. Spring Boot fills that list from Filter beans and FilterRegistrationBean beans, sorted by their order, lower values first.
Spring Security appears in that list as one entry, a DelegatingFilterProxy named springSecurityFilterChain, registered by Boot at order -100 by default (spring.security.filter.order). It delegates to FilterChainProxy, which holds one or more SecurityFilterChain beans. Each SecurityFilterChain is its own ordered list of security filters: SecurityContextHolderFilter, which sets up the security context for the request, the authentication filters, ExceptionTranslationFilter, and AuthorizationFilter, which decides whether the request is allowed. So there is a chain inside a chain, and each has its own ordering rules.
- Container filter chainsorted by
@Order, Ordered orFilterRegistrationBeanorder - springSecurityFilterChain at -100
DelegatingFilterProxy->FilterChainProxy - SecurityFilterChain
SecurityContextHolderFilter, authentication filters,ExceptionTranslationFilter,AuthorizationFilter - Other container filtersa
@Componentfilter with no order lands here, after security - DispatcherServletcontrollers, interceptors
5 layers, from the top down. Step through them one at a time.
2Reproducing the Problem
Here is the kind of filter that fails. It reads an API key and sets an authenticated user in the SecurityContextHolder, the per-thread holder Spring Security reads the current user from.
The filter is registered by Boot as an ordinary container filter with the lowest precedence, so it sits after springSecurityFilterChain. The security chain runs first, finds no authentication, and AuthorizationFilter rejects the request. Our filter would have set the user, but it runs too late, or not at all for rejected requests.
3Why Moving It Earlier in the Container Chain Does Not Work Either
The natural next attempt is @Order(Ordered.HIGHEST_PRECEDENCE), so the filter runs before Spring Security. That still fails, more subtly. When the security chain starts, SecurityContextHolderFilter replaces whatever is in the SecurityContextHolder with the context loaded from the security context repository, which for a stateless API is empty. The user our filter set is overwritten, and the request is again anonymous. And at the end of the chain, the holder is cleared.
So the security context is owned by the security chain. Authentication must happen inside it, after the context is set up and before the authorization decision. Servlet filter order is the right tool for filters that do not depend on security, such as request ids or access logs, and the wrong tool for authentication.
6 messages in order. Play them, or step through them one at a time.
Outside the security chain, the user is either set too late or overwritten.
4The Fix: Add the Filter Inside the Security Chain
We add the filter to the SecurityFilterChain at a known position, relative to a built-in filter, with addFilterBefore or addFilterAfter. Placing it before UsernamePasswordAuthenticationFilter is the common choice for custom authentication: it runs after the security context is set up and well before authorization.
Notice that the filter is now created with new inside the configuration and is not a @Component. That is deliberate: if it is not a bean, Boot cannot register it as a container filter as well. A stateless API like this one does not need to save the context between requests; a filter that should log a user in across requests would also have to save the context through a SecurityContextRepository, which is explicit in current Spring Security versions.
5When the Filter Must Stay a Bean: Disable the Second Registration
Sometimes the filter needs to be a bean, for example because it has injected dependencies. Then Boot will also register it as a container filter, and it runs in both chains. If it extends OncePerRequestFilter, its already-filtered request attribute makes the second run skip quietly, which hides the double registration; a plain Filter really runs twice, doing the work twice and possibly at the wrong moment. We tell Boot not to register it with the container by declaring a disabled FilterRegistrationBean.
6Follow-up questions
How do I see the actual order of security filters?
Spring Security logs the filters of each SecurityFilterChain at startup at DEBUG level, and with logging.level.org.springframework.security=TRACE it logs each filter as a request passes through it, which shows exactly where a custom filter sits and where a request was rejected. For container filters, Boot's startup log at DEBUG lists the registered filters and their mappings.
addFilterBefore or addFilterAt?
addFilterBefore and addFilterAfter place our filter next to a built-in one and keep both. addFilterAt places it at the same order as the built-in filter, but does not replace it, so both run in an undefined relative order. For custom authentication, addFilterBefore a known filter is clearer.
Should a custom auth filter call an AuthenticationManager?
For anything more than a simple key lookup, yes. The filter builds an unauthenticated token from the request and passes it to an AuthenticationManager, and a custom AuthenticationProvider validates it. That keeps validation logic in one testable place and reuses Spring Security's event and exception handling.
7Conclusion
In this answer, we saw that Spring Security is a single container filter at order -100 that contains its own SecurityFilterChain, that container filter order comes from @Order, Ordered or FilterRegistrationBean.setOrder, and that a @Component filter with no order runs after security, while one ordered before it has its user overwritten by SecurityContextHolderFilter. We fixed it by adding the filter inside the security chain with addFilterBefore, and prevented a second container registration by not making it a bean or by disabling its FilterRegistrationBean.
Open with
There are two filter chains. Spring Security is registered with the container as one filter, a DelegatingFilterProxy to FilterChainProxy, at order minus 100 by default, and inside it runs its own SecurityFilterChain: SecurityContextHolderFilter, the authentication filters, ExceptionTranslationFilter and AuthorizationFilter.
Then, if they want more
Container filter order in Boot comes from @Order, the Ordered interface, or FilterRegistrationBean.setOrder. My custom filter was a @Component with no order, so Boot registered it as a container filter with the lowest precedence, after the whole security chain. By then AuthorizationFilter had already decided the request was anonymous and returned 401.
Ordering it first in the container chain does not help either, because SecurityContextHolderFilter replaces the context with the one from the repository, which is empty for a stateless API, so the user I set is overwritten.
The fix is to put the filter inside the security chain, where the security context is managed: in the SecurityFilterChain bean I call addFilterBefore(apiKeyFilter, UsernamePasswordAuthenticationFilter.class), so it runs after the context is set up and before authorization, and in the filter I create a new SecurityContext with the authentication and set it on the holder.
Then I make sure it does not also run as a container filter. Either I do not make it a bean and create it in the configuration, or, if it must be a bean, I declare a FilterRegistrationBean for it with setEnabled(false).
Otherwise it is registered twice; OncePerRequestFilter would hide that by skipping the second run, but a plain filter would run twice. To check the order I turn on Spring Security trace logging.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Jakarta EE specifications
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Your custom authentication filter runs after Spring Security's filters and sees no user....": https://scrivio-interview-library.netlify.app/java/servlets/586ef03d96/