The @CrossOrigin annotation tells Spring Boot to accept cross-origin browser requests for a controller or a single handler method. Spring then adds the CORS response headers (such as Access-Control-Allow-Origin) and answers the browser's preflight OPTIONS request for you. Without it, a browser blocks JavaScript on one origin from reading responses of an API on another.
It is the quickest way to open one endpoint to a known frontend. For an API-wide policy, use a global CORS configuration instead.
A browser applies the same-origin policy: a page from http://localhost:3000 may not read a response from http://localhost:8080, because scheme, host or port differ. CORS (Cross-Origin Resource Sharing) is the opt-in mechanism: the server answers with headers that name the origins it trusts.
For simple requests the browser sends the request and checks the response headers. For requests with a JSON body, custom headers such as Authorization, or methods like PUT and DELETE, the browser first sends a preflight:
Only if the response allows the origin, method and headers does the browser send the real request. CORS is enforced by browsers; it is not an authentication mechanism and does not stop curl or other servers.
Place the annotation on a class to cover all of its handlers, or on a method for one endpoint:
list() inherits the class-level origin. create() declares its own list of origins, and Spring combines the class and method settings: attributes with several values are merged, while single-value attributes such as allowCredentials and maxAge on the method override the class.
| Attribute | Meaning | Default |
|---|---|---|
origins / value | Allowed origins, or * | All origins |
originPatterns | Origin patterns such as https://*.example.com | None |
methods | Allowed HTTP methods | Those of the handler mapping |
allowedHeaders | Request headers the client may send | All headers |
exposedHeaders | Response headers JavaScript may read | None |
allowCredentials | Whether cookies and auth headers are sent | Not enabled (since Spring 5.0) |
maxAge | Seconds a preflight result may be cached | 1800 |
allowCredentials is a String attribute ("true" or "false"), so you write allowCredentials = "true".
If the frontend must send cookies or an Authorization header with credentials: "include", enable credentials and name the origin explicitly:
Spring rejects origins = "*" together with allowCredentials = "true", and browsers refuse it too. To match several subdomains with credentials, use originPatterns = "https://*.example.com". Avoid blanket patterns on authenticated endpoints.
Repeating the annotation on many controllers is error-prone. A global rule keeps the policy in one place:
Spring merges global and annotation settings, so @CrossOrigin can refine the global rule for one endpoint.
If Spring Security is on the classpath, the annotation alone is not enough for secured endpoints: the security filter chain sees the unauthenticated preflight first and rejects it. Enable CORS in the chain with http.cors(Customizer.withDefaults()), which makes Spring Security use your MVC CORS configuration or a CorsConfigurationSource bean.
@CrossOrigin without arguments in production. It allows every origin; list your real frontend origins.http://localhost:3000/ does not match http://localhost:3000.500 or 401 response without CORS headers also shows up in the browser as a CORS error, so check the network tab and server logs.Yes. The same annotation works on annotated controllers in WebFlux; functional endpoints use a CorsWebFilter or the CorsConfigurationSource instead.
No. Requests to the same scheme, host and port are same-origin and CORS does not apply.
Usually Spring Security blocks it. Enable cors() in the security configuration so the preflight is handled before authentication.
For a public, unauthenticated read-only API, * is acceptable. For anything that uses cookies or tokens, list the exact origins.
@CrossOrigin enables CORS for a controller or handler method by adding the right response headers and answering preflight requests. Name your real origins, enable credentials only when needed, prefer a global configuration for whole APIs, and turn on CORS in Spring Security when endpoints are secured.