Quick revision
The short answer
Spring Security 6 changed the default request handler to XorCsrfTokenRequestAttributeHandler, which expects a masked token, while the XSRF-TOKEN cookie holds the raw token that the SPA copies into the header, so the comparison fails and every POST gets 403. The token is also loaded lazily, so the cookie may never be written until something reads it. The fix is the SPA configuration from the Spring Security reference, which accepts the raw value from the header and forces the token to load, or the csrf.spa() shortcut in recent versions; then test the whole flow including the first POST after login.
What to remember
In this answer, we traced the Spring Security 6 SPA 403 to two changed defaults. The XOR request handler expects a masked token while the XSRF-TOKEN cookie holds the raw one, and the deferred token means the cookie may not be written at all. The SPA handler from the reference accepts the raw header value and loads the token on each response, or csrf.spa() in recent versions does the same, and a browser-level test of the first POST after login keeps it working.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Spring Security 6 changed the default request handler to XorCsrfTokenRequestAttributeHandler, which expects a masked token, while the XSRF-TOKEN cookie holds the raw token that the SPA copies into the header, so the comparison fails and every POST gets 403. The token is also loaded lazily, so the cookie may never be written until something reads it. The fix is the SPA configuration from the Spring Security reference, which accepts the raw value from the header and forces the token to load, or the csrf.spa() shortcut in recent versions; then test the whole flow including the first POST after login.
Why this question is asked
This is one of the most common upgrade surprises from Spring Security 5 to 6. Nothing in the SPA changed, the cookie and header names are the same, and yet every POST fails. Interviewers ask about it because the cause sits in two small defaults that changed together, and understanding them shows that we know how the CSRF machinery works rather than just copying a configuration. In this answer, we will recap the cookie-to-header pattern, reproduce the 403, explain the masked token and the deferred token, apply the fix, and finish with a test that covers the flow.
1A Quick Recap of the Cookie-to-Header Pattern
A single-page app cannot read a hidden form field that the server rendered, so Spring Security offers CookieCsrfTokenRepository. It stores the CSRF token in a cookie named XSRF-TOKEN. With withHttpOnlyFalse(), JavaScript can read that cookie, and the SPA copies its value into an X-XSRF-TOKEN header on each state-changing request. Angular and Axios do this automatically for those exact names.
The protection holds because an attacker's page on another origin cannot read our cookies, so it cannot produce the header. The server compares the header value with the token it knows, and rejects the request if they differ.
2Reproducing the 403 After the Upgrade
The configuration below worked on Spring Security 5.8 with its old defaults, and fails on 6.x.
Notice that the SPA does exactly what it did before. The difference is entirely in how the server now reads the header value.
3Why the Comparison Fails: the Masked Token
In 6.0, the default CsrfTokenRequestHandler became XorCsrfTokenRequestAttributeHandler. It protects against BREACH, an attack that recovers secrets from compressed HTTPS responses by watching their size. To do that, whenever the token is rendered into a page, it is XOR-combined with fresh random bytes, and the page gets the random bytes plus the result, encoded together. When a request comes back, the handler expects that masked form, splits it, and XORs again to recover the real token.
The XSRF-TOKEN cookie, however, holds the raw token, not a masked one. When the SPA sends the raw value in the header, the handler treats it as a masked value, "unmasks" it into garbage, and the comparison fails. So the cookie and the handler disagree about the format, and the request is rejected with 403.
| Piece | Spring Security 5.8 default | Spring Security 6 default |
|---|---|---|
| Token in XSRF-TOKEN cookie | Raw | Raw |
| Value the handler expects back | Raw | Masked (XOR with random bytes) |
| SPA sends cookie value in header | Matches | Does not match -> 403 |
| When the token is loaded | On every request | Lazily, only when read |
4The Second Cause: the Token Is Loaded Lazily
Also in 6.0, the token became deferred: CsrfFilter no longer loads or generates the token on every request, only when something calls for it. For a server-rendered page, the form rendering reads it, so the cookie gets written. A SPA served as static files never reads it, so on a fresh session the XSRF-TOKEN cookie may simply not exist yet. The first POST then has no header value at all and fails too.
This explains a symptom that confuses people: sometimes the cookie is there, left over from an older session, and sometimes it is not, especially right after login, when Spring Security changes the session and the token.
5The Fix: Configure the Handler for SPAs
The Spring Security reference documentation describes a handler for single-page apps. It keeps BREACH protection for values that come from rendered pages, but accepts the raw cookie value when it arrives in the header, and it makes sure the token is loaded on each response so the cookie gets written.
What changed is that a header value is compared as a raw token, which is what the cookie gives the SPA, while form parameters still go through the masked path. The token.get() call replaces the separate filter that older versions of the documentation added just to force the cookie to be written. Spring Security 7 adds a shortcut, csrf.spa(), that applies this configuration in one call; check your version before relying on it.
6Testing the Complete SPA Flow
Unit tests with MockMvc and with(csrf()) will pass whatever the handler does, because they inject a valid token directly. The bug lives in the real browser flow, so the test must follow it: load the app, log in, then perform the first POST, and check the XSRF-TOKEN cookie after each step. A browser test with Playwright or Selenium, or an HTTP-level test that keeps cookies and copies the cookie into the header the way Axios does, catches both the masking mismatch and the missing cookie. The first POST after login matters most, because login replaces the token.
- Load the appResponse should set XSRF-TOKEN
- Log inToken is replaced; new cookie expected
- First POST after loginHeader = new cookie value -> 200
- POST without headerMust still be 403
4 steps in order. Play them, or step through them one at a time.
7Follow-up questions
Why not just switch back to CsrfTokenRequestAttributeHandler for everything?
It works, and the reference mentions it as an opt-out, but it gives up BREACH protection for tokens rendered into pages. If the application also has server-rendered forms, the SPA handler keeps that protection while accepting the raw header value.
Is it safe that the cookie is readable by JavaScript?
Yes, for this token. It is not a credential; it only proves the request came from a page of our origin, because only our pages can read our cookies. Our session cookie must still be HttpOnly.
The cookie is present but POSTs still fail with the new handler. What next?
Check that the header name matches, X-XSRF-TOKEN by default for the cookie repository, that the cookie path and domain make it visible to the SPA, and that the client copies the latest cookie after login rather than a cached value.
8Conclusion
In this answer, we traced the Spring Security 6 SPA 403 to two changed defaults. The XOR request handler expects a masked token while the XSRF-TOKEN cookie holds the raw one, and the deferred token means the cookie may not be written at all. The SPA handler from the reference accepts the raw header value and loads the token on each response, or csrf.spa() in recent versions does the same, and a browser-level test of the first POST after login keeps it working.
Open with
There are two changes in Spring Security 6 behind this. The main one is that the default CSRF request handler became XorCsrfTokenRequestAttributeHandler. To protect against BREACH, it masks the token with random bytes every time it is rendered, and when a request comes back it expects that masked form and unmasks it.
Then, if they want more
But CookieCsrfTokenRepository puts the raw token in the XSRF-TOKEN cookie, and the SPA copies that raw value into the X-XSRF-TOKEN header. The handler unmasks a value that was never masked, gets garbage, and the request fails with 403. The second change is that the token is now deferred, only loaded when something reads it.
A server-rendered form reads it, but a static SPA never does, so on a fresh session the cookie may not even be set, especially right after login.
The fix is the SPA configuration from the Spring Security reference: a custom CsrfTokenRequestHandler that uses the plain handler when the token comes in a header and the XOR handler otherwise, and calls the token supplier in handle so the cookie gets written on every response.
In Spring Security 7 there is a csrf.spa() shortcut for this, but I would check the version. Then I would test the real flow in a browser test: load the app, log in, do the first POST after login, and also check that a POST without the header still gets 403.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Security 5, Spring Security 6, Spring Security 7
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- OWASP Top Ten
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "After upgrading to Spring Security 6, a single-page app using...": https://scrivio-interview-library.netlify.app/spring-boot/web-security-threats/d1df713fc2/