How to enable CORS in Spring Boot - Not working
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
CORS in Spring Boot often appears to be broken because configuration is applied in one layer while requests are blocked in another. Effective setup requires alignment between MVC configuration, security rules, and preflight request handling. A partial setup usually passes local tests but fails in browser integration.
High-quality implementation guidance should survive framework upgrades and operational stress, not only pass one local run. Treat each approach as a contract with clear assumptions, diagnostics, and rollback options.
CORS Configuration Layers
1. Configure Global MVC CORS Rules
Start with explicit mappings for allowed origins, methods, and headers. Keep origin lists concrete rather than broad wildcards in authenticated APIs.
Start with a minimal baseline and verify expected behavior in one clear success scenario. Keeping first steps simple makes debugging faster and lowers onboarding cost for contributors.
2. Enable CORS In Spring Security Chain
If Spring Security is enabled, configure CORS there too. Security filters may otherwise block preflight requests before MVC rules apply.
Once baseline behavior is correct, harden around boundary conditions, resource management, and failure handling. This is usually where production incidents are prevented.
3. Verify With Real Preflight Requests
Browser behavior depends on preflight negotiation details. Validate with network inspection and explicit origin headers instead of only server-side unit tests.
Add repeatable checks in automation, including one happy-path, one edge-case, and one failure-path test. Fast CI feedback keeps these guarantees from regressing during refactors.
Operational readiness also includes recovery planning. Feature toggles, rollback procedures, and clear observability reduce risk when real-world traffic reveals unexpected conditions.
A maintainable solution should define explicit contracts for expected input and behavior under failure. Document which errors are retriable, which require operator action, and which should fail fast. Clear contracts reduce ambiguity between teams and prevent divergent handling in different modules.
Testing depth should include realistic scenarios, not only happy paths. Add one representative production-like case, one malformed-input case, and one dependency failure case. Keep these tests in CI so upgrades and refactors cannot silently alter behavior. Fast repeatable verification is the strongest defense against regression.
Operational safety also deserves first-class treatment. Before rollout, prepare a rollback procedure, feature gating plan, and the telemetry needed for rapid diagnosis. Even correct implementations can fail in real environments due to traffic shape, timing, or infrastructure drift. Recovery planning ahead of time keeps incidents shorter and less disruptive.
Long-term reliability also depends on ownership and documentation. Record who owns this path, where alerts should route, and how operators can reproduce the issue quickly in a non-production environment. Small runbook notes near implementation details often prevent repeated investigation cycles and reduce handoff friction during on-call rotations.
Track one service-level metric that reflects user impact, and review it after each change to confirm the fix improved real behavior rather than only synthetic tests.
Common Pitfalls
- Configuring CORS only in MVC while security chain still blocks preflight requests.
- Using wildcard origins with credentials and expecting browser acceptance.
- Forgetting to allow
OPTIONSfor preflight negotiation. - Testing only with server tools that skip browser CORS enforcement rules.
- Applying path mappings that do not match actual API routes.
Summary
- Align CORS settings across MVC and Spring Security layers.
- Handle preflight requests explicitly for authenticated APIs.
- Use precise allowed origin policies in production environments.
- Validate behavior with real browser-origin request flows.

