CSRF protection not working with Spring Security 6
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Cross-Site Request Forgery (CSRF) is a common web application vulnerability that allows an attacker to perform actions on behalf of a different user without their consent or knowledge. It is a significant security concern, especially for state-changing requests like form submissions, password changes, or any transaction where user authentication is required.
Spring Security, a powerful and customizable authentication and access control framework, has built-in CSRF protection mechanisms. Spring Security 6, the latest version in the series, provides these features, but sometimes, developers encounter issues where CSRF protection does not work as intended. This article dives into the nuances of CSRF protection in Spring Security 6, includes examples of potential problems, and suggests possible fixes.
Understanding CSRF Protection
CSRF protection works by including a secret token with each request that performs a state-changing operation. This token must match the token stored on the user's server-side session. The essential process involves:
- Generating a CSRF Token: The server generates a unique CSRF token and passes it to the client. This usually happens when rendering a form or a state-changing page.
- Storing the Token: The token is commonly stored in the user's session.
- Submitting the Token: The client submits the token back to the server whenever making a state-changing request.
- Validating the Token: The server compares the received token with the one stored in the session. If they match, the request is processed; otherwise, it is denied.
Common CSRF Issues in Spring Security 6
Misconfigured CSRF Settings
One of the most frequent issues arises from misconfiguration. Spring Security 6 may introduce changes that affect the default behavior or require modified configuration settings. Ensure that the CSRF configuration is explicitly enabled.
Example:
- Enable CSRF Explicitly: Always specify CSRF protection when setting up security configurations.
- Ensure Token Synchronization: Double-check that the server-generated token appears correctly in client-side scripts and forms.
- Review Documentation: Pay attention to updated Spring Security documentation for version-specific changes or deprecations.
Related reading
- Custom attribute not passed into ID_TOKEN created by AWS Cognito
- Custom path for Hashicorp Vault kubernetes auth method does not work uisng cli
- Dealing with CORS using the Contour Kubernetes Ingress
- Debugging process for Kafka SSL security
- CSV API for Java
- Custom annotation on controller methods to intercept request and validate
- Decoding and verifying JWT token using System.IdentityModel.Tokens.Jwt
- Decoding Kubernetes secret

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.