Quick revision
The short answer
Spring Boot registers every bean that implements Filter with the servlet container automatically, so JwtLoggingFilter runs once as a plain servlet filter, outside Spring Security and possibly before authentication, and once inside the security chain where addFilterBefore put it. The fix is to stop the automatic registration, by not making the filter a bean or by declaring a FilterRegistrationBean for it with setEnabled(false). Extending OncePerRequestFilter can hide the symptom but not the double registration.
What to remember
In this answer, we saw that JwtLoggingFilter ran twice because Spring Boot registers every Filter bean with the servlet container, and addFilterBefore registers it again inside Spring Security's chain. The container's copy can run before authentication, which explains the missing user. We fixed it by not making the filter a bean, or by keeping the bean and disabling its container registration with a FilterRegistrationBean, and we saw why OncePerRequestFilter only hides the problem. The actuator mappings endpoint and security debug logging show both lists of filters.
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 Boot registers every bean that implements Filter with the servlet container automatically, so JwtLoggingFilter runs once as a plain servlet filter, outside Spring Security and possibly before authentication, and once inside the security chain where addFilterBefore put it. The fix is to stop the automatic registration, by not making the filter a bean or by declaring a FilterRegistrationBean for it with setEnabled(false). Extending OncePerRequestFilter can hide the symptom but not the double registration.
Why this question is asked
This bug catches experienced developers because each half of the setup is correct on its own. @Component is how we usually make things beans, and addFilterBefore is the documented way to add a filter to Spring Security. Together they register the same filter twice in two different chains. Interviewers ask it because the explanation shows whether we know how Spring Boot builds the servlet filter chain and how it relates to Spring Security's own chain. In this answer, we will reproduce the problem, see the two registrations side by side, explain why one run happens before authentication, go through the fixes, and see how to check which filters are registered.
1Two Different Filter Chains
There are two filter chains in a Spring Boot web application. The first is the servlet container's chain, Tomcat's list of filters that runs for every request. The second is Spring Security's chain, the filters held by FilterChainProxy, which is itself registered in the container's chain as one filter named springSecurityFilterChain.
Spring Boot fills the container's chain automatically: any bean of type Filter in the application context is registered with the container, mapped to all URLs. This is convenient, since a @Component filter just works, but it also means that being a bean and being in the security chain are two separate registrations that both take effect.
2Reproducing the Problem
Here is the setup the team wrote, with a log line that shows whether a user has been authenticated yet.
The filter is a bean, so Boot registers it with Tomcat. It is also added to the security chain. The same object now sits in two places, so it runs twice per request.
3Why One Run Happens Before Authentication
The container's copy runs at its position in the container's chain, which depends on its order relative to the springSecurityFilterChain registration (Spring Boot gives that one a fixed order, -100 by default; check your version). Our bean has @Order(Ordered.HIGHEST_PRECEDENCE), added to make logging run early, so like any filter ordered before the security filter, it runs before Spring Security has looked at the token, so there is no authentication yet. That is the 'user: none' line.
The copy inside the security chain runs exactly where addFilterBefore put it, just before AuthorizationFilter and therefore after BearerTokenAuthenticationFilter, so the authentication is there. The two runs therefore see different worlds, which is often how the bug is first noticed: logs that show a missing user and a present user for the same request.
7 messages in order. Play them, or step through them one at a time.
Being a bean registers the filter with Tomcat; addFilterBefore registers it again with Spring Security.
4Fix 1: Do Not Make It a Bean
The simplest fix is to remove @Component and create the filter where we add it. It is no longer in the application context, so Boot does not register it with the container.
If the filter needs dependencies, such as a metrics registry, we pass them through its constructor from the configuration method, which receives them as bean parameters.
5Fix 2: Keep the Bean but Disable Its Registration
Sometimes the filter must stay a bean, perhaps because it is shared or has many dependencies. Then we tell Boot not to register it with the container by declaring a FilterRegistrationBean for it, disabled.
Notice that the bean still exists and is still added to the security chain; only the automatic container registration is switched off. This is the same technique Spring Boot users apply to any filter that belongs only inside the security chain.
6Why OncePerRequestFilter Is Not the Real Fix
OncePerRequestFilter is a Spring base class that stores a marker attribute on the request the first time it runs and skips itself if it sees that marker again during the same request. If JwtLoggingFilter extended it, the second run would be skipped, and the logs would show one line.
But which run survives? The first one, which in our case is the container's copy before authentication. So the filter would now run only in the wrong place, always seeing no user, and the configuration would look correct because it runs once. OncePerRequestFilter is useful for its real purpose, avoiding repeated runs across forward and include dispatches, but it should not be used to paper over a double registration.
7Checking Which Filters Are Registered
To see the container's filters, the actuator mappings endpoint (/actuator/mappings, when exposed) lists the servlet filters with their URL patterns, and a duplicate shows up as our filter appearing there in addition to springSecurityFilterChain. To see the security chain's filters, debug logging for org.springframework.security prints each chain at startup. Comparing the two lists makes a double registration obvious, and a startup test that asserts our filter is absent from the servlet filters list keeps it from coming back.
8Follow-up questions
Does the same problem happen with custom authentication filters?
Yes, and it is worse: an authentication filter registered with the container may run outside the security chain where the SecurityContext is not managed, or authenticate twice. Any filter meant only for the security chain should either not be a bean or have its container registration disabled.
Could we instead remove addFilterBefore and keep @Component?
Then the filter runs only as a container filter, ordered relative to the springSecurityFilterChain registration rather than to specific security filters. That is fine for filters that do not care about security, such as request logging without the user, but not for one that must run after authentication.
How do we set the order of a container filter?
With @Order on the bean or with FilterRegistrationBean.setOrder(...). Lower values run earlier, and the security filter chain's registration order is configurable with spring.security.filter.order.
9Conclusion
In this answer, we saw that JwtLoggingFilter ran twice because Spring Boot registers every Filter bean with the servlet container, and addFilterBefore registers it again inside Spring Security's chain. The container's copy can run before authentication, which explains the missing user. We fixed it by not making the filter a bean, or by keeping the bean and disabling its container registration with a FilterRegistrationBean, and we saw why OncePerRequestFilter only hides the problem. The actuator mappings endpoint and security debug logging show both lists of filters.
Open with
The filter is registered twice in two different chains. Spring Boot automatically registers every bean that implements Filter with the servlet container, so because JwtLoggingFilter is a @Component, Tomcat runs it for every request.
Then, if they want more
Separately, addFilterBefore puts the same object into Spring Security's filter chain. So it runs once as a plain servlet filter and once inside FilterChainProxy.
The container copy runs at its own order relative to the springSecurityFilterChain registration, and if it is ordered earlier it runs before the bearer token has been authenticated, which is why one of the runs sees no user.
To fix it I would either remove @Component and create the filter inside the security configuration, passing any dependencies through the constructor, or keep it as a bean and declare a FilterRegistrationBean for it with setEnabled(false), which stops Boot registering it with the container while it stays in the security chain.
I would not rely on OncePerRequestFilter, because it only skips the second run, and here the first run is the wrong one, so the filter would end up running only before authentication. To check, I would look at the servlet filters in the actuator mappings endpoint and the security chain in the startup debug log.
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
- Spring Security reference
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 "A team adds a custom JwtLoggingFilter annotated with @Component and also adds it to the...": https://scrivio-interview-library.netlify.app/spring-boot/security-architecture/cd717cf62c/