CorsConfiguration is the Spring Framework class (org.springframework.web.cors.CorsConfiguration) that holds one set of Cross-Origin Resource Sharing rules: which origins, HTTP methods and headers a browser may use against your endpoints, whether credentials are allowed, and how long the browser may cache the preflight answer. Spring MVC, Spring WebFlux and Spring Security all read these rules to decide whether to add CORS headers or reject a cross-origin request.
It is a plain configuration holder. It does not process requests itself: filters, handler mappings or CorsRegistry apply it.
Browsers enforce the same-origin policy. A page served from https://app.example.com cannot read a response from https://api.example.com unless the API answers with the right Access-Control-Allow-* headers. For requests that could change data (non-simple methods or custom headers), the browser first sends a preflight OPTIONS request and only continues if the server approves.
CorsConfiguration models the server's answer to those checks, so you declare the policy once and the framework generates the headers.
| Property | Response header | Meaning |
|---|---|---|
allowedOrigins | Access-Control-Allow-Origin | Exact origins that may call the API |
allowedOriginPatterns | Access-Control-Allow-Origin | Origins matched with patterns such as https://*.example.com |
allowedMethods | Access-Control-Allow-Methods | HTTP methods permitted |
allowedHeaders | Access-Control-Allow-Headers | Request headers the client may send |
exposedHeaders | Access-Control-Expose-Headers | Response headers JavaScript may read |
allowCredentials | Access-Control-Allow-Credentials | Whether cookies and HTTP authentication are allowed |
maxAge | Access-Control-Max-Age | How long a preflight result may be cached |
Two rules are easy to get wrong. First, if allowCredentials is true, you cannot use the wildcard * in allowedOrigins, allowedHeaders or allowedMethods; list explicit values, or use allowedOriginPatterns for origins. Second, an empty configuration allows nothing, so call applyPermitDefaultValues() or set the properties you need.
The central method is a CorsConfigurationSource bean built from a UrlBasedCorsConfigurationSource, which maps URL patterns to a CorsConfiguration.
With Spring Security on the classpath, enable it in the filter chain so CORS runs before authentication. Spring Security picks up the CorsConfigurationSource bean automatically:
If you do not use Spring Security, you can use the simpler WebMvcConfigurer callback, which builds the same configuration for you:
For a single controller or method, the @CrossOrigin annotation creates a CorsConfiguration for just that handler.
WebFlux uses the reactive counterparts in org.springframework.web.cors.reactive. CorsConfiguration itself is the same class; only the source and filter differ.
Alternatively, implement WebFluxConfigurer.addCorsMappings to use CorsRegistry.
For each request, the framework combines the global configuration with any handler-level one (@CrossOrigin), checks the Origin header against the allowed values and then:
Access-Control-Allow-* headers and does not call your controller.Requests without an Origin header are not CORS requests and pass through untouched.
Common causes are using * with allowCredentials(true), missing a header in allowedHeaders, or Spring Security rejecting the preflight before CORS runs. Call http.cors(...) in Security and check the exact response headers in the browser network panel.
allowedOrigins takes exact origins (or *). allowedOriginPatterns supports wildcards inside the host, such as https://*.example.com, and works with credentials.
It is enforced by the browser, not the server. It protects users, but it does not stop tools like curl from calling your API, so you still need authentication and authorisation.
No. List only the origins you control, especially when cookies or tokens are involved.
CorsConfiguration describes your CORS policy: allowed origins, methods, headers, credentials and preflight cache time. Register it through a CorsConfigurationSource (and http.cors() with Spring Security), the WebMvcConfigurer registry or CorsWebFilter in WebFlux, and keep the lists explicit and as narrow as your clients allow.