liveBook · Manning

Filter chain

As explained in Spring Security in Action, Second Edition

A filter chain is an ordered sequence of request-processing filters in which each filter can delegate processing to the next filter.

A filter chain is an ordered sequence of HTTP filters. Each filter receives a request, performs its responsibility, and delegates processing to the next filter. Spring Security supplies filters for concerns such as HTTP Basic authentication, CSRF protection, and CORS rules, while applications can add custom filters. section 5.0 section 5.1

Customize a chain by choosing where a filter acts:

  • Add a filter before an existing filter when logic must run before authentication. section 5.1
  • Add a filter after an existing filter for post-authentication logging, tracing, or notifications. section 5.3
  • Add a filter at an existing filter’s location when replacing its responsibility, such as implementing static-key, signed-request, or one-time-password authentication. section 5.4
  • Assign placement through the filter order; filters sharing an order value have no guaranteed relative order. section 5.1

Logging authenticated requests

A custom AuthenticationLoggingFilter implements Filter, reads a Request-Id header, logs the successful authentication event, and calls filterChain.doFilter(request, response). HttpSecurity then installs it with addFilterAfter(..., BasicAuthenticationFilter.class). section 5.3

Figure 5.1 The request is passed to the filter chain. Every filter engages a manager to execute particular logic upon the request and then passes it down the line to the subsequent filter in the chain.

Use the diagram to trace how each filter engages its responsibility and passes the request to the next filter. section 5.0

How Spring Security in Action, Second Edition covers it

How deep it goes
Explains request delegation, filter ordering, Filter implementation, and before, after, and replacement insertion strategies.
Examples
Uses BasicAuthenticationFilter placement, Request-Id logging, static-key authentication, curl requests, and HTTP 401 responses.
What it assumes
Assumes basic Spring and Java knowledge while introducing HTTP filter objects, doFilter parameters, and request delegation.
What you can do after
Operate/configure Spring Security chains by implementing Filter and inserting custom filters before, after, or at existing positions.
How it connects
Ties HTTP filters to authentication, authorization, CSRF protection, CORS, and request logging.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Security in Action, Second Edition. The section links point to the parts of the book it draws on.