@CrossOrigin is a Spring MVC annotation that enables Cross-Origin Resource Sharing (CORS) for a controller class or handler method. It tells the browser that a page served from a different origin, for example a React app on http://localhost:3000, is allowed to call your API on another origin.
Without CORS headers, browsers block such calls by the same-origin policy, even though the server received the request. @CrossOrigin adds the required response headers for you.
An origin is the combination of scheme, host and port. A frontend at https://app.example.com calling https://api.example.com is cross-origin. For many requests, such as those with JSON bodies or an Authorization header, the browser first sends a preflight OPTIONS request asking which methods and headers are allowed. Only if the response carries matching Access-Control-Allow-* headers does it send the real request.
CORS is enforced by browsers only. It does not protect your API from curl, scripts or other servers, so it is not a substitute for authentication.
Place it on a single handler method, or on the class to cover every method in the controller.
Method-level settings are combined with class-level ones, and attributes with the same name on the method refine or replace those on the class. By default @CrossOrigin allows the methods mapped on the handler, all headers, and a preflight cache age of 1800 seconds.
| Attribute | Purpose | Notes |
|---|---|---|
origins / value | Allowed origins | Exact origins such as https://app.example.com |
originPatterns | Allowed origin patterns | For example https://*.example.com |
methods | Allowed HTTP methods | Defaults to the methods of the mapping |
allowedHeaders | Request headers allowed | Defaults to all |
exposedHeaders | Response headers readable by JavaScript | None by default |
allowCredentials | Allow cookies and auth credentials | Not enabled by default |
maxAge | Preflight cache duration in seconds | Default 1800 |
A common source of errors is mixing credentials with a wildcard. When allowCredentials = "true", the response cannot use * for Access-Control-Allow-Origin; you must list explicit origins or use originPatterns.
Allowing * together with credentials would let any website make authenticated calls on behalf of a logged-in user, which is why browsers and Spring reject it. Never reflect arbitrary origins in production.
Repeating the annotation on many controllers becomes hard to maintain. Define one policy with WebMvcConfigurer.
For reactive applications use CorsWebFilter or the same addCorsMappings approach in WebFluxConfigurer.
If Spring Security is on the classpath, its filter chain runs before MVC, so preflight requests can be rejected with 401 or 403 before @CrossOrigin is consulted. Enable CORS in the security configuration so it uses your MVC configuration or a CorsConfigurationSource bean.
With cors(withDefaults()), Spring Security looks for a bean named corsConfigurationSource, or falls back to the MVC CORS setup.
Yes. It is part of Spring Web and works the same in recent versions, with originPatterns available since Spring 5.3.
Common causes are Spring Security rejecting the preflight, an origin that does not exactly match (check scheme and port), or a redirect or error response that lacks CORS headers.
@CrossOrigin without arguments safe?With no attributes it allows all origins for the mapped methods, but not credentials. Specify explicit origins in production.
No. CORS is a browser mechanism and has no effect on server-to-server requests.
@CrossOrigin configures CORS headers per controller or method so browsers permit cross-origin calls to your Spring API. Prefer explicit origins, avoid wildcards with credentials, centralise the policy in a global configuration, and enable cors() in Spring Security so preflight requests are not blocked.