Video in progress
How is the filter chain in Spring Security structured, and which pattern does it match?
Spring Security plugs in as a single servlet filter (DelegatingFilterProxy → FilterChainProxy), and inside it finds the SecurityFilterChain matching the URL and runs its filters one after another. That's Chain of Responsibility, and the outer hookup is Proxy/Delegate.
📖 Answer
The structure goes layer by layer:
- The servlet container knows
DelegatingFilterProxy: it's an ordinary filter, but it hands the work to a bean in the Spring context. - That bean is
FilterChainProxy. It holds severalSecurityFilterChains (for example, JWT for/api/**, form login for the rest). - The first matching chain is chosen and its filters run in order: CSRF, logout, authentication,
ExceptionTranslationFilter,AuthorizationFilter...
Each filter does only its own job. For example, if there's no token in the header, BearerTokenAuthenticationFilter does nothing and passes the request on; if the token is invalid, it cuts the chain and returns 401.
For debugging:
logging:
level:
org.springframework.security: TRACE
The log shows which filters each request went through and where it stopped. At startup the list of filters in the chain is printed.
The most common mistake when adding your own filter is choosing the wrong place. Give an exact position with http.addFilterBefore(myFilter, UsernamePasswordAuthenticationFilter.class). And if you make the filter a @Component, Spring Boot also registers it as a plain servlet filter, so it runs twice.
Sign up to mark studied questions and save your progress.