csrf().disable() turns off Spring Security's Cross-Site Request Forgery protection, so state-changing requests (POST, PUT, PATCH, DELETE) no longer need a CSRF token. It is safe only for stateless APIs that authenticate with something the browser does not send automatically, such as a bearer token in the Authorization header. It is dangerous for any app that relies on session cookies.
In Spring Security 6 and Spring Boot 3, the chained csrf().disable() form is deprecated. You write http.csrf(csrf -> csrf.disable()) or http.csrf(AbstractHttpConfigurer::disable) instead.
A CSRF attack tricks a logged-in user's browser into sending a request to your site, for example from a hidden form on a malicious page. The browser attaches the session cookie automatically, so the server cannot tell the request was not intended by the user.
Spring Security defends against this by default. For unsafe HTTP methods it requires a token that an attacker's page cannot read, sent as a header or a form field. Requests without a valid token get 403 Forbidden. Safe methods (GET, HEAD, OPTIONS, TRACE) are not checked, which is why GET requests must never change state.
Many tutorials call csrf().disable() because a REST client such as curl or Postman fails with 403 on the first POST. Disabling CSRF makes the error go away, but it also removes a protection that browser-based, cookie-authenticated applications need. The 403 is the framework doing its job, not a bug.
The lambda DSL is the supported style. The old no-argument csrf() chain was deprecated in Spring Security 6.1 and removed in 7.0.
This is a stateless resource server: no session, no cookie authentication, every request carries a JWT. With no ambient credentials a forged cross-site request has nothing to ride on, so CSRF tokens add nothing.
| Situation | Disable CSRF? | Reason |
|---|---|---|
Stateless API, JWT or API key in the Authorization header | Usually yes | The browser does not attach the header on its own |
| Server-rendered app with form login and session cookie | No | Cookies are sent automatically, which is the CSRF attack surface |
| Single-page app using a session cookie | No | Keep CSRF on and use a cookie-based token repository |
| JWT stored in a cookie | No | It is still an ambient credential |
| Service-to-service calls with mTLS or tokens | Usually yes | No browser involved |
The test is simple. If a browser can authenticate a request without your JavaScript explicitly adding a credential, you need CSRF protection.
Rather than disabling it, expose the token to the front end. Spring can store it in an XSRF-TOKEN cookie that the script reads and sends back in the X-XSRF-TOKEN header.
Since Spring Security 6, the token is loaded lazily and masked with BREACH protection, so SPAs need this handler configuration to read the cookie value reliably. Check the current reference documentation for the exact recommended handler for your version.
If one webhook endpoint cannot send a token, exempt that path and keep protection everywhere else.
Authenticate such endpoints another way, for example with a signature check on the payload.
Yes, when the API is stateless and clients send credentials explicitly, such as a bearer token. It is not safe if authentication relies on session cookies.
CSRF protection is on by default and rejects unsafe requests without a valid token. Send the token, ignore specific paths, or disable CSRF if your API is stateless.
No. They are separate. CORS controls which origins may read responses; CSRF protects state-changing requests from forged submissions.
The lambda form csrf(csrf -> csrf.disable()) is. The old chained csrf().disable() is deprecated in Spring Security 6.1 and removed in 7.0.
csrf().disable() removes the token check on unsafe HTTP methods. Use it for stateless, header-authenticated APIs and keep it on for anything that authenticates with cookies. Write it with the lambda DSL, and prefer targeted ignoringRequestMatchers over disabling protection globally.