Spring Boot Security CORS
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
CORS (Cross-Origin Resource Sharing) errors in Spring Boot happen when the browser blocks requests from a frontend on one domain to a backend on another. Spring Security adds an extra layer — it processes requests before your CORS configuration takes effect, so CORS must be configured within the security filter chain. The fix is to call cors() on the HttpSecurity object and define a CorsConfigurationSource bean.
The Error
When CORS is misconfigured, the browser blocks the request with:
This happens because the browser sends a preflight OPTIONS request, and Spring Security rejects it before your controller or CORS filter runs.
Fix: Spring Boot 3.x / Spring Security 6.x
The key is cors(cors -> cors.configurationSource(...)) — this tells Spring Security to apply CORS before authentication checks, allowing preflight OPTIONS requests through.
Spring Boot 2.x / Spring Security 5.x
Alternative: @CrossOrigin on Controllers
For simple cases, annotate controllers directly:
@CrossOrigin works for simple apps but does not integrate with Spring Security's filter chain. For secured endpoints, use the SecurityFilterChain approach.
Global CORS via WebMvcConfigurer
This configures CORS at the MVC level. However, when Spring Security is present, you must also enable CORS in the security config — otherwise, Security's filter chain rejects the preflight before MVC sees it.
CORS Configuration Options
Development: Allow All Origins
Use @Profile("dev") to restrict permissive CORS to development only. Never allow * origins with credentials in production.
Common Pitfalls
- CORS not in Security config: Defining CORS only in
WebMvcConfigurerwithout enabling it inHttpSecuritymeans Spring Security blocks preflightOPTIONSrequests before MVC processes them. Always add.cors()to your security config. allowedOrigins("*")withallowCredentials(true): This combination is not allowed by the CORS spec. UseallowedOriginPatterns("*")instead, or list specific origins.- Forgetting OPTIONS in allowed methods: Preflight requests use the
OPTIONSmethod. If your allowed methods list does not include it, preflights fail. Most configurations should includeOPTIONS. - CSRF blocking POST/PUT/DELETE: Even with CORS configured, Spring Security's CSRF protection rejects non-GET requests without a token. Disable CSRF for stateless APIs or include the CSRF token in requests.
- Multiple CORS configurations conflicting: Having both
@CrossOrigin,WebMvcConfigurer, andSecurityFilterChainCORS configs can cause unexpected behavior. Use one approach consistently.
Summary
- Configure CORS inside
SecurityFilterChainwith.cors(cors -> cors.configurationSource(...))for Spring Security integration - Define a
CorsConfigurationSourcebean with specific allowed origins, methods, and headers @CrossOriginworks for simple cases without Spring SecurityWebMvcConfigurer.addCorsMappings()configures MVC-level CORS but needs Security-level CORS too when Spring Security is present- Never use
allowedOrigins("*")withallowCredentials(true)— useallowedOriginPatternsinstead - Use
@Profile("dev")for permissive development CORS configurations
Related reading
- Spring boot Security Disable security
- Spring Boot Security No 'Access-Control-Allow-Origin' header is present on the requested resource Error
- Spring Boot Spring Security Hierarchical Roles
- Spring Boot Swagger UI. Set JWT token
- Spring boot show sql parameter binding?
- Spring Boot shutdown hook
- Spring Boot Unit Tests with JWT Token Security
- spring boot with spring security Error creating bean with name 'securityFilterChainRegistration

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.