CorsConfiguration is the Spring class that holds a single CORS policy: the allowed origins, methods and headers, whether credentials are allowed, which response headers are exposed, and how long browsers may cache the preflight answer. It does not enforce anything by itself. Spring MVC, Spring WebFlux and Spring Security read it through a CorsConfigurationSource and apply it to incoming requests.
You use it when @CrossOrigin on single controllers is too fine-grained and you want one policy for a whole API, or when Spring Security must answer preflight requests before authentication runs.
org.springframework.web.cors.CorsConfiguration is a plain value object. Each setter maps to a response header that the browser checks before it lets JavaScript read a cross-origin response.
| Method | CORS response header | Purpose |
|---|---|---|
setAllowedOrigins / addAllowedOrigin | Access-Control-Allow-Origin | Exact origins that may call the API |
setAllowedOriginPatterns | Access-Control-Allow-Origin | Origin patterns such as https://*.example.com |
setAllowedMethods | Access-Control-Allow-Methods | HTTP methods allowed in a cross-origin call |
setAllowedHeaders | Access-Control-Allow-Headers | Request headers the client may send |
setExposedHeaders | Access-Control-Expose-Headers | Response headers JavaScript may read |
setAllowCredentials | Access-Control-Allow-Credentials | Whether cookies and HTTP auth may be sent |
setMaxAge | Access-Control-Max-Age | How long the preflight result may be cached |
An origin is the combination of scheme, host and port, so https://example.com and https://example.com:8443 are different origins.
The following snippet creates a policy for a single-page app served from https://app.example.com.
Only the listed origin may call the API, only those methods and request headers are accepted, and the browser may read Location and X-Request-Id from responses. Cookies are allowed, and a successful preflight is cached for an hour (setMaxAge also accepts a Long number of seconds).
A configuration with no values set allows nothing. The method applyPermitDefaultValues() fills in permissive defaults for the simple methods (GET, HEAD, POST), all headers and a 30 minute max age, but it does not enable credentials.
A CorsConfiguration becomes active only after you register it for URL paths. The most common way in Spring MVC is a CorsConfigurationSource bean, which Spring Boot picks up when Spring Security is on the classpath. Without Spring Security, use WebMvcConfigurer, which wraps a CorsConfiguration internally.
CorsRegistry builds a CorsConfiguration per path pattern, so the two styles express the same policy. Use @CrossOrigin on a controller only for exceptions to this global rule; Spring combines the global and the local configuration.
Spring Security runs its filter chain before Spring MVC. A browser sends the preflight OPTIONS request without credentials, so if security rejects it with 401, the real request never happens. Enabling CORS in the security chain makes Spring Security handle the preflight first.
.cors(Customizer.withDefaults()) tells Spring Security to look up the CorsConfigurationSource bean and insert a CorsFilter ahead of the authentication filters. The older http.cors().and() style was removed in Spring Security 7; the lambda form above works in 6.x and later.
Browsers refuse a wildcard * for origins, headers or methods when credentials are allowed, and Spring enforces this rule too. This combination throws an IllegalArgumentException when the configuration is validated:
Use setAllowedOriginPatterns when you need to match many origins with credentials, and keep the patterns as narrow as you can:
Echoing every origin back with credentials enabled lets any site act on behalf of a logged-in user, so avoid *-style patterns for authenticated APIs.
@CrossOrigin annotation?No. @CrossOrigin is a per-controller or per-method declaration that Spring turns into a CorsConfiguration internally. Using CorsConfiguration directly gives you one central place to define the policy.
Usually Spring Security is not told to use your CORS settings. Add .cors(Customizer.withDefaults()) and expose a CorsConfigurationSource bean, otherwise the preflight request is rejected as unauthenticated.
No. CORS is enforced by browsers only. Tools such as curl or server-side code ignore it, so you still need authentication and authorization.
You need setAllowCredentials(true) on the server and credentials: "include" in the fetch call on the client, and the allowed origin must be explicit rather than *.
CorsConfiguration describes a CORS policy: origins, methods, headers, exposed headers, credentials and preflight cache time. Register it through a CorsConfigurationSource or WebMvcConfigurer so it applies globally, enable CORS in Spring Security so preflight requests are answered before authentication, and never combine a wildcard origin with credentials.