@CrossOrigin lets a browser page served from one origin call your Spring endpoint on another origin by adding CORS (Cross-Origin Resource Sharing) response headers. It is a Spring Web MVC and WebFlux annotation, not a Spring Security one, but when Spring Security is on the classpath you must also enable CORS support in the security filter chain, or preflight requests are rejected with 401 or 403 before the annotation is ever consulted.
Browsers enforce the same-origin policy: a script from https://app.example.com cannot read responses from https://api.example.com unless the API opts in with headers such as Access-Control-Allow-Origin. @CrossOrigin declares that opt-in on a controller class or handler method.
The annotation supports these attributes:
| Attribute | Meaning |
|---|---|
origins / originPatterns | Allowed origins; patterns allow wildcards such as https://*.example.com |
methods | Allowed HTTP methods (defaults to the mapped method) |
allowedHeaders | Request headers the client may send |
exposedHeaders | Response headers the script may read |
allowCredentials | Allow cookies and Authorization credentials |
maxAge | Seconds the browser may cache the preflight result |
Placed on a method it applies to that endpoint only; on a class it applies to all handlers in the class, and the two are merged.
A cross-origin request that is not "simple" (for example a JSON POST, or any call with an Authorization header) is preceded by a preflight OPTIONS request. Browsers send that preflight without credentials. Spring Security's filter chain runs before Spring MVC, so an unauthenticated OPTIONS request is blocked by the authorization rules, and MVC never gets to apply @CrossOrigin.
The fix is to tell Spring Security to handle CORS itself, using the same MVC configuration. In Spring Security 6 and later:
With .cors(Customizer.withDefaults()), Spring Security adds a CorsFilter at the start of the chain. It looks for a bean named corsConfigurationSource (or a CorsFilter bean, or MVC CORS configuration) and answers preflight requests before authentication runs. The older http.cors().and() style and WebSecurityConfigurerAdapter were removed in Spring Security 6.
In real applications it is cleaner to define CORS rules once, as a bean, instead of repeating @CrossOrigin on every controller. This is also the most reliable way to cover endpoints that Spring Security itself serves, such as the login or OAuth2 endpoints.
Alternatively, implement WebMvcConfigurer.addCorsMappings to configure MVC CORS, and Spring Security will pick it up through .cors(Customizer.withDefaults()).
allowCredentials = true with origins = "*". Browsers reject it and Spring throws an IllegalArgumentException. List explicit origins or use originPatterns.curl, scripts, or other servers, so authentication and authorization are still required.403 on OPTIONS almost always means .cors(...) is missing from the security chain.You can verify the setup without a browser:
A correct response is 200 with headers like these:
If the headers are missing, check that the origin matches exactly, including scheme and port, and that the security chain has CORS enabled.
No. It comes from Spring Web (org.springframework.web.bind.annotation). Spring Security only needs cors() enabled so the CORS check happens before authentication.
Use a global CorsConfigurationSource for consistent rules across the application. Use @CrossOrigin for a single endpoint that needs an exception.
Postman does not enforce CORS. The browser blocks the response when the CORS headers are missing or wrong.
For public APIs without credentials you can use *. For anything that uses cookies or tokens, list the specific origins.
@CrossOrigin marks controllers or handlers as callable from other browser origins, but with Spring Security you must also call http.cors(Customizer.withDefaults()) and ideally define a CorsConfigurationSource bean. Use explicit origins, avoid wildcard origins with credentials, and remember that CORS is a browser rule, not an access-control mechanism.