Sub topic 2
How requests flow through the security filter chain
This chapter assumes you can write a Spring Boot REST controller, you know what a servlet filter is at a basic level, and you understand HTTP status codes and headers. You do not need to know any Spring Security configuration yet. The next chapter shows how to define the chain in code.
Why a filter chain
Spring Security does not live inside your controllers. It sits in front of them, as a set of servlet filters. A servlet filter is a component that sees every HTTP request before the servlet (in Spring MVC, the DispatcherServlet) and sees the response on the way back. Because the filters run first, a rejected request never reaches your controller code.
Authentication and authorization both happen inside these filters. Authentication is working out who the caller is. Authorization is deciding whether that caller may do what they asked.
The pieces
Three objects connect the servlet container to Spring Security's own filters:
DelegatingFilterProxy(in a plain Spring app) or Spring Boot's equivalent registration is a single filter the servlet container knows about. It hands each request to a Spring bean.FilterChainProxyis that bean. It holds one or more security filter chains and picks one for each request.SecurityFilterChainis an ordered list of security filters plus a rule for which requests it applies to. Each chain you define as a bean is one entry.
FilterChainProxy checks the chains in order and uses the first one whose request matcher accepts the request. Only that chain runs. This matters when you declare several chains, for example one for /api/** and one for everything else. Put the more specific chain first, or the general one will capture every request.
The order inside one chain
Each chain runs its filters in a fixed order. You can add or replace filters, but the order is part of the design. A typical chain for a stateless API contains these filters, from first to last:
| Filter | Job |
|---|---|
| `SecurityContextHolderFilter` | Loads the `SecurityContext` for this request and clears it afterwards |
| `HeaderWriterFilter` | Adds security response headers |
| `CorsFilter` | Handles CORS pre-flight requests (only if you call `cors()`) |
| `CsrfFilter` | Rejects unsafe requests without a valid CSRF token (only if enabled) |
| `BasicAuthenticationFilter` or `BearerTokenAuthenticationFilter` | Reads credentials from the request and authenticates them |
| `ExceptionTranslationFilter` | Turns security exceptions into 401 or 403 responses |
| `AuthorizationFilter` | Decides whether the authenticated caller may access this URL |
The exact list depends on what you enable. You can print the real list for your application by setting logging.level.org.springframework.security=DEBUG. At startup and on each request, the log shows the filters in order.
What happens to one request
The diagram follows a request to a protected URL. Authentication filters read credentials and store the result. AuthorizationFilter is last, so it sees whatever identity the earlier filters established.
The ExceptionTranslationFilter makes the 401 and 403 outcomes possible. It wraps the filters after it in a try block. When AuthorizationFilter throws an AccessDeniedException, ExceptionTranslationFilter catches it. If the caller is anonymous, it starts the authentication entry point, which usually produces 401. If the caller is authenticated but not permitted, it produces 403.
Common errors
- Chain order surprises. Two
SecurityFilterChainbeans without an@Orderannotation can end up in an unexpected order. Add@Orderand give the narrow matcher a lower number. - Expecting filters to see your beans' exceptions. Security filters run before the controller. An exception thrown inside a controller is handled by Spring MVC, not by
ExceptionTranslationFilter. - Adding a custom filter in the wrong place. Use
addFilterBeforeoraddFilterAfterrelative to a known filter. A filter added without a position can run after authorization, when it is too late to authenticate.
Exercise
A request to /api/orders arrives with no credentials, and the URL requires authentication. Which filter turns the resulting AccessDeniedException into a 401 response, and which filter threw the exception?
Show answer
AuthorizationFilter throws the exception because the caller is anonymous and the rule requires authentication. ExceptionTranslationFilter catches it and, because the caller is anonymous, starts the authentication entry point, which returns 401.
Exercise
You define two SecurityFilterChain beans. The first matches every request, and the second matches /api/**. Which chain handles a request to /api/orders, and how do you fix the setup if that is not what you want?
Show answer
The first chain handles it, because FilterChainProxy uses the first chain whose matcher accepts the request. To fix it, annotate both beans with @Order and give the /api/** chain the lower number, or restrict the first chain's matcher so it no longer covers /api/**.