liveBook · Manning

Servlet filter

As explained in Spring Security in Action, Second Edition

An HTTP filter is a web-processing component that intercepts and can modify or reject HTTP requests and responses.

An HTTP filter intercepts a request, performs focused processing, and either forwards it to the next filter or ends the exchange. Spring Security assembles filters into an ordered chain, allowing each component to handle responsibilities such as authentication, authorization, CSRF protection, or CORS rules. section 5.0 section 5.1

A custom filter implements the jakarta.servlet.Filter interface and overrides doFilter(ServletRequest, ServletResponse, FilterChain). The request supplies input, the response can be changed, and filterChain.doFilter(request, response) forwards processing. Omitting that call stops the chain. section 5.1

Use a filter when request processing must happen at a precise point:

  • Validate a required Request-Id header before authentication; return HTTP 400 Bad Request when it is missing or blank. section 5.2
  • Insert a custom filter before an existing filter with HttpSecurity.addFilterBefore(). section 5.2
  • Insert a filter after an existing filter with addFilterAfter(), for example to log requests that pass authentication. section 5.3
  • Choose the simplest implementation that meets the requirement; use GenericFilterBean or OncePerRequestFilter only when their added behavior is needed. section 5.5

Reject malformed requests before authentication

A RequestValidationFilter reads the Request-Id header. If the value is absent or blank, it sets HttpServletResponse.SC_BAD_REQUEST and returns. Otherwise, it calls filterChain.doFilter(request, response). Register it with addFilterBefore(new RequestValidationFilter(), BasicAuthenticationFilter.class) in a SecurityFilterChain bean. A request without the header returns 400; a request carrying Request-Id:12345 reaches the endpoint. section 5.2

Avoid duplicate authentication logging

An AuthenticationLoggingFilter can log the Request-Id after BasicAuthenticationFilter. When repeated execution must be prevented, extend OncePerRequestFilter and implement doFilterInternal(HttpServletRequest, HttpServletResponse, FilterChain), forwarding after logging. section 5.3 section 5.5

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.

Visualizes the request moving through managers and successive filters in the chain. section 5.0

How Spring Security in Action, Second Edition covers it

How deep it goes
Descends from chain ordering to doFilter implementation and stops short of enumerating every framework-provided filter.
Examples
Uses Request-Id validation, authentication-event logging, curl requests, HTTP 400 responses, and OncePerRequestFilter.
What it assumes
Assumes basic Spring and Java knowledge while teaching servlet filter parameters, forwarding, rejection, and ordering.
What you can do after
Build it from scratch by implementing Filter, control chain placement with HttpSecurity, and select a Spring base class when justified.
How it connects
Connects filter chains with authentication, authorization, CSRF protection, CORS, and servlet request-response handling.

Also explained in

Compare how 3 books explain servlet filter →

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.