liveBook · Manning

CsrfTokenRequestHandler

As explained in Spring Security in Action, Second Edition

CsrfTokenRequestHandler is a Spring Security interface for managing how a generated CSRF token is associated with an HTTP request.

CsrfTokenRequestHandler is a Spring Security contract that controls how a generated CSRF token is associated with an HTTP request. It complements CsrfToken, which describes the token, and CsrfTokenRepository, which creates, stores, and loads it. section 9.3 section 9.4

Use it when customizing CSRF token management, such as pairing a database-backed repository with CsrfTokenRequestAttributeHandler in the security filter chain. The repository can store tokens outside the HTTP session, while the handler governs their request-facing association.

Configure a request handler

http.csrf(c -> { c.csrfTokenRepository(customTokenRepository); c.csrfTokenRequestHandler( new CsrfTokenRequestAttributeHandler() ); }); This configuration uses a custom repository and explicitly selects the request handler. A GET request can generate and store a token; a later POST must provide the matching X-CSRF-TOKEN value or receive HTTP 403.

How Spring Security in Action, Second Edition covers it

How deep it goes
Defines the contract and shows filter-chain selection, but does not detail a custom handler implementation.
Examples
Uses a database-backed token repository, explicit request-handler configuration, and curl requests that succeed or fail based on token presence.
What you can do after
Operate/configure CSRF protection with a selected request handler and a custom token repository in Spring Security.
How it connects
Connects CSRF protection with CsrfToken, CsrfTokenRepository, CsrfTokenRequestAttributeHandler, and the HTTP 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.