Skip to content

Section 1: Security Filter Chain Architecture & Internals

1. Enterprise Security Fundamentals: The CIA Triad & AAA Framework

In enterprise software engineering, securing backend systems requires more than simple username/password prompts. Security architecture is built upon formal defense-in-depth frameworks:

flowchart TD
    subgraph CIA["The CIA Security Triad"]
        C["Confidentiality<br/>Data accessible only to authorized entities.<br/>(Encryption in transit/at rest, PII masking)"]
        I["Integrity<br/>Data protected from unauthorized tampering.<br/>(Cryptographic signatures, CSRF defenses, HMACs)"]
        A["Availability<br/>Systems and data remain accessible.<br/>(DDoS mitigation, rate limiting, connection pooling)"]
    end

    subgraph AAA["The AAA Security Model"]
        AUTHN["Authentication (Who are you?)<br/>Validating proof of identity via credentials, tokens, or certs."]
        AUTHZ["Authorization (What can you do?)<br/>Evaluating permissions, roles, and resource-level access."]
        AUDIT["Accounting & Non-Repudiation (What did you do?)<br/>Immutable audit logging ensuring actions cannot be denied."]
    end

    C --- AUTHN
    I --- AUTHZ
    A --- AUDIT

When engineers add spring-boot-starter-security to a Spring Boot project, the framework instantly establishes an automated security perimeter around every HTTP endpoint:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

Default Auto-Configuration Behavior

Out of the box, without writing a single line of custom code, Spring Boot's SecurityAutoConfiguration activates:

  1. Universal Route Protection: Every HTTP endpoint in the application requires authentication.
  2. Default In-Memory User: A single user named user is provisioned by UserDetailsServiceAutoConfiguration.
  3. Generated Password: A cryptographically random UUID password is generated on startup and logged to the console:
    Using generated security password: 9f8a3c4b-12d4-4e56-8a9b-0c1d2e3f4a5b
    
  4. Dual Authentication Entrypoints: Both an auto-generated HTML login page (/login) via DefaultLoginPageGeneratingFilter and HTTP Basic Authentication headers are active.
  5. CSRF Protection: Enabled by default on all HTTP mutating methods (POST, PUT, DELETE, PATCH).

2. The Servlet Bridge: DelegatingFilterProxy & FilterChainProxy

Spring Security is fundamentally built upon the standard Java EE / Jakarta EE Servlet Filter specification. However, a fundamental architectural dilemma exists between the Servlet container and the Spring framework:

  • The Servlet Container (Tomcat / Jetty) manages its own filter lifecycle (init, doFilter, destroy) outside the Spring ApplicationContext.
  • Spring beans (such as UserDetailsService, PasswordEncoder, repositories) live inside the Spring IoC Container and support dependency injection.

How does a standard Servlet container filter leverage Spring beans to perform authentication and authorization?

Spring resolves this impedance mismatch using the DelegatingFilterProxy:

flowchart LR
    REQ["Incoming HTTP Request"]

    subgraph SERVLET_ENGINE["Servlet Container (Tomcat)"]
        FILT1["LoggingFilter"]
        DFP["DelegatingFilterProxy<br/>(Registered in web.xml / Servlet Context)"]
        SERVLET["DispatcherServlet"]
    end

    subgraph SPRING_CONTAINER["Spring IoC ApplicationContext"]
        FCP["FilterChainProxy<br/>(Bean name: 'springSecurityFilterChain')"]

        subgraph CHAIN1["SecurityFilterChain 1 (e.g. /api/**)"]
            F1["CorsFilter"]
            F2["CsrfFilter"]
            F3["BasicAuthFilter"]
            F4["AuthorizationFilter"]
        end
    end

    REQ --> FILT1
    FILT1 --> DFP
    DFP -->|"Delegates doFilter() call"| FCP
    FCP --> CHAIN1
    CHAIN1 --> SERVLET

1. DelegatingFilterProxy

DelegatingFilterProxy is a standard jakarta.servlet.Filter registered with the servlet container. When an HTTP request enters, DelegatingFilterProxy does not execute security checks itself. Instead, it looks up a Spring bean named springSecurityFilterChain from the WebApplicationContext and delegates the doFilter() invocation to that bean.

2. FilterChainProxy

The bean named springSecurityFilterChain is an instance of FilterChainProxy. It is the central orchestration hub of Spring Security. FilterChainProxy holds a collection of SecurityFilterChain instances and determines which chain should process the current request based on URL pattern matchers.

Why FilterChainProxy is Essential

By routing requests through FilterChainProxy instead of registering security filters directly with the servlet container, Spring Security achieves:

  1. Dynamic Chain Selection: Multiple distinct SecurityFilterChain beans can exist for different paths (e.g., stateless /api/** vs session-based /web/**).
  2. Unified Security Context Management: Guarantees that SecurityContextHolder is reset and cleared after every request, preventing cross-request identity leakage.
  3. Bypass Optimization: Allows requests matching WebSecurity.ignoring() to bypass security processing entirely without overhead.

3. Anatomy of the SecurityFilterChain

A SecurityFilterChain is a defined sequence of servlet filters ordered by strict precedence. Each filter encapsulates a distinct responsibility in the security lifecycle:

flowchart TD
    REQ["HTTP Request Invocations"]

    subgraph SFC["Standard Security Filter Chain Sequence"]
        direction TB
        F1["1. DisableEncodeUrlFilter<br/>Prevents session ID URL rewriting"]
        F2["2. SecurityContextHolderFilter<br/>Populates SecurityContext from session repository"]
        F3["3. HeaderWriterFilter<br/>Injects security headers (X-Frame-Options, CSP, HSTS)"]
        F4["4. CorsFilter<br/>Evaluates origin and handles preflight OPTIONS"]
        F5["5. CsrfFilter<br/>Validates anti-CSRF token on mutating requests"]
        F6["6. LogoutFilter<br/>Intercepts /logout, clears session and tokens"]
        F7["7. UsernamePasswordAuthenticationFilter<br/>Extracts form login credentials from POST /login"]
        F8["8. DefaultLoginPageGeneratingFilter<br/>Renders built-in HTML login form"]
        F9["9. BasicAuthenticationFilter<br/>Decodes Base64 'Authorization: Basic' credentials"]
        F10["10. RequestCacheAwareFilter<br/>Restores original requested URL after login"]
        F11["11. AnonymousAuthenticationFilter<br/>Assigns AnonymousAuthenticationToken if unauthenticated"]
        F12["12. ExceptionTranslationFilter<br/>Converts AuthenticationException -> 401, AccessDenied -> 403"]
        F13["13. AuthorizationFilter<br/>Final gatekeeper: evaluates requestMatchers() rules"]
    end

    TARGET["Target Controller / Resource<br/>WelcomeController.sayWelcome()"]

    REQ --> F1
    F1 --> F2
    F2 --> F3
    F3 --> F4
    F4 --> F5
    F5 --> F6
    F6 --> F7
    F7 --> F8
    F8 --> F9
    F9 --> F10
    F10 --> F11
    F11 --> F12
    F12 --> F13
    F13 -->|"Access Granted"| TARGET
    F12 -.->|"401 / 403 Handled"| REQ

Filter Ordering Guarantees

Filters in Spring Security must execute in exact sequence. For example, CorsFilter must execute before CsrfFilter and authentication filters so preflight OPTIONS requests are never rejected for lacking authentication or CSRF tokens. Similarly, ExceptionTranslationFilter must sit above AuthorizationFilter to catch security exceptions and translate them into standard HTTP responses.


4. The End-to-End Authentication Architecture

When an incoming request presents credentials (e.g., HTTP Basic Authorization header or POST form data), Spring Security executes a multi-tiered dispatch protocol:

sequenceDiagram
    autonumber
    actor Client as HTTP Client
    participant Filter as BasicAuthenticationFilter
    participant Manager as AuthenticationManager (ProviderManager)
    participant Provider as DaoAuthenticationProvider
    participant UserDetailsSvc as UserDetailsService
    participant PasswordEnc as PasswordEncoder
    participant SecCtx as SecurityContextHolder

    Client->>Filter: GET /welcome (Header: Authorization Basic dXNlcjoxMjM0NQ==)
    Filter->>Filter: Decode Base64 -> username="user", password="secret"
    Filter->>Filter: Create unauthenticated UsernamePasswordAuthenticationToken
    Filter->>Manager: authenticate(unauthenticatedToken)
    Manager->>Provider: authenticate(unauthenticatedToken)
    Provider->>UserDetailsSvc: loadUserByUsername("user")
    UserDetailsSvc-->>Provider: returns UserDetails (hashed password & authorities)
    Provider->>PasswordEnc: matches(rawPassword, encodedPassword)

    alt Password Matches
        PasswordEnc-->>Provider: true
        Provider->>Provider: Create authenticated UsernamePasswordAuthenticationToken<br/>(credentials cleared, authorities attached)
        Provider-->>Manager: returns authenticatedToken
        Manager-->>Filter: returns authenticatedToken
        Filter->>SecCtx: getContext().setAuthentication(authenticatedToken)
        Filter->>Client: 200 OK ("Welcome to Spring Application with security")
    else Password Mismatch / User Not Found
        PasswordEnc-->>Provider: false
        Provider--xManager: throws BadCredentialsException
        Manager--xFilter: propagates BadCredentialsException
        Filter->>Client: 401 Unauthorized (WWW-Authenticate: Basic)
    end

Detailed Component Roles in Authentication

1. Authentication Token Contract

The Authentication interface represents both an authentication request and an authenticated principal. It defines:

  • getPrincipal(): The identity of the user (e.g., username or UserDetails object).
  • getCredentials(): The secret proof of identity (e.g., password or token). Once authenticated, credentials are erased to prevent heap exposure.
  • getAuthorities(): A collection of GrantedAuthority objects (roles and permissions).
  • isAuthenticated(): Boolean flag indicating whether credentials have been verified.

2. AuthenticationManager & ProviderManager

AuthenticationManager is the core strategy interface for authentication:

public interface AuthenticationManager {
    Authentication authenticate(Authentication authentication)
            throws AuthenticationException;
}
The standard implementation is ProviderManager, which delegates authentication to a chain of configured AuthenticationProvider instances.

3. AuthenticationProvider

The component responsible for executing actual authentication logic. It supports:

  • authenticate(Authentication): Validates credentials.
  • supports(Class<?>): Declares which token type it can authenticate (e.g., UsernamePasswordAuthenticationToken.class).

4. SecurityContextHolder & SecurityContext

The storage hub for authentication metadata. By default, SecurityContextHolder stores the SecurityContext in a ThreadLocal, ensuring thread-isolated security context access throughout the request lifecycle:

// Accessing authenticated user anywhere in the execution thread:
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String currentUsername = auth.getName();

ThreadLocal & Thread-Pool Caching Hazards

Because SecurityContextHolder uses ThreadLocal storage by default, asynchronous threads (e.g., @Async, CompletableFuture, or custom ExecutorService pools) will not inherit the security context unless explicitly configured with SecurityContextHolder.MODE_INHERITABLETHREADLOCAL or using DelegatingSecurityContextExecutor. At the end of every HTTP request, FilterChainProxy cleans the ThreadLocal to prevent identity leaks across pooled threads.