In Spring, @CrossOrigin marks a controller class or handler method as accepting cross-origin requests. Spring builds a CORS policy from the annotation and applies it in the request-mapping infrastructure, in both Spring MVC and Spring WebFlux. It gives you per-endpoint control, where a global configuration gives you one policy for the whole application.
The annotation is read at startup and merged with any global CORS configuration, so you can set a broad default and override it for a few endpoints.
@CrossOrigin is processed when Spring registers the handler mapping (RequestMappingHandlerMapping). For each mapped method it reads the annotation on the method and its class, creates a CorsConfiguration, and stores it with the handler. At request time, Spring checks the Origin header against that configuration, answers preflight OPTIONS requests automatically, and adds the Access-Control-* headers to the real response.
This means you do not write an OPTIONS handler yourself. A request whose origin is not allowed receives a 403 response and no CORS headers, which browsers then report as a CORS error.
The annotation can sit on a type or a method. Both levels are combined:
Combination rules: list-valued attributes (origins, methods, allowedHeaders, exposedHeaders) are merged, so delete accepts both https://books.example.com and https://admin.example.com. Single-value attributes (allowCredentials, maxAge) from the method win over the class.
origins (or value): exact origins such as https://books.example.com. * allows any origin and is the default when none is given.originPatterns: patterns such as https://*.example.com, useful with credentials where * is not allowed.methods: the HTTP methods allowed for cross-origin calls. By default it follows the methods of the request mapping.allowedHeaders: headers the browser may send, such as Authorization or Content-Type. Default is all.exposedHeaders: response headers that scripts may read. Without this, scripts see only the CORS-safelisted response headers.allowCredentials: "true" lets the browser send cookies and HTTP authentication. It is a String and it is off by default since Spring 5.0.maxAge: how many seconds browsers may cache the preflight result. The default is 1800 seconds (30 minutes).An origin of * combined with allowCredentials = "true" is rejected; Spring throws an IllegalArgumentException at startup.
| Aspect | @CrossOrigin | Global (WebMvcConfigurer, CorsConfigurationSource) |
|---|---|---|
| Scope | One controller or method | URL patterns, whole app |
| Where policy lives | Next to the code it affects | One central class |
| Works with Spring Security filters | Only if cors() is enabled in the chain | Yes, through CorsConfigurationSource |
| Best for | A few exceptions | A consistent API-wide policy |
Spring merges both. A global rule for /api/** plus @CrossOrigin(maxAge = 60) on one method gives that method a 60 second preflight cache and the global origins.
Spring Security filters run before MVC handlers, and a browser's preflight carries no credentials. If the security chain demands authentication for OPTIONS requests, the preflight fails with 401 or 403 before @CrossOrigin is consulted. Enable CORS support in the chain:
With default settings Spring Security uses a CorsConfigurationSource bean if present, or the MVC CORS configuration (including @CrossOrigin) otherwise.
On annotated controllers in WebFlux, @CrossOrigin works exactly as above. Functional endpoints (RouterFunction) do not read the annotation. For those, register a CorsWebFilter with a UrlBasedCorsConfigurationSource built from a CorsConfiguration.
It allows all origins, all request headers, the methods of the mapping, no credentials, and a preflight cache of 1800 seconds. That is convenient for development and too open for most production APIs.
No. CORS limits what browser scripts can read; it does not block requests from servers, scripts or tools. Use authentication and authorization for real protection.
Put it on the controller class. For Spring Data REST, use a global CORS configuration or the RepositoryRestConfigurer CORS registry instead.
Common causes are a mismatched origin (scheme, port or trailing slash), Spring Security rejecting the preflight, or an error response generated before the handler runs.
@CrossOrigin attaches a CORS policy to a Spring controller or method, merged with any global configuration. Use explicit origins, set allowCredentials only when needed, rely on a global CorsConfigurationSource for API-wide rules, and enable CORS in Spring Security so preflight requests succeed.