CorsConfiguration (org.springframework.web.cors.CorsConfiguration) is the Spring class that holds one set of Cross-Origin Resource Sharing rules: which origins, HTTP methods and headers a browser may use against your server, whether credentials are allowed, which response headers are exposed, and how long a preflight result may be cached. Spring's CORS support reads these settings and adds the correct Access-Control-* headers to responses.
You rarely use the class on its own. It is the value object behind @CrossOrigin, WebMvcConfigurer.addCorsMappings, CorsFilter and Spring Security's CORS support. The examples target Spring Framework 6 and Spring Boot 3 or later.
CorsConfigurationBrowsers apply the same-origin policy: a page from https://app.example.com cannot read a response from https://api.example.com unless the API opts in with CORS headers. For requests with custom headers or methods such as PUT and DELETE, the browser first sends an OPTIONS preflight request and only continues if the response allows it.
CORS is enforced by browsers, not servers. It does not protect your API from curl or other servers, so it is not a substitute for authentication.
CorsConfiguration| Property | Purpose |
|---|---|
allowedOrigins | Exact origins such as https://app.example.com, or * for all |
allowedOriginPatterns | Patterns such as https://*.example.com, usable with credentials |
allowedMethods | Methods allowed, for example GET, POST, PUT, DELETE |
allowedHeaders | Request headers the client may send, for example Content-Type, Authorization |
exposedHeaders | Response headers that scripts may read, beyond the default safe ones |
allowCredentials | Whether cookies and HTTP authentication are allowed |
maxAge | Seconds a browser may cache the preflight result |
One rule catches many projects. When allowCredentials is true, you cannot use * for allowedOrigins, allowedHeaders or allowedMethods. Spring rejects that combination at runtime. List explicit origins, or use allowedOriginPatterns.
CorsConfiguration objects are mapped to URL paths through a CorsConfigurationSource, most often UrlBasedCorsConfigurationSource.
This allows the single origin to call every path under /api/ with credentials, and tells browsers to cache the preflight response for one hour. setMaxAge(Duration) is available in current Spring versions; older ones take a Long in seconds.
For a simpler setup without CorsConfiguration, use WebMvcConfigurer. It builds the same configuration internally.
@CrossOriginWhen Spring Security is on the classpath, the security filter chain runs before MVC, so a blocked preflight never reaches your CORS mapping. Enable CORS in the security configuration and it will use the CorsConfigurationSource bean above.
Access-Control-Allow-Origin.Origin includes scheme, host and port, because http://localhost:3000 differs from http://localhost:5173.OPTIONS response. A 401 or 403 there usually means Spring Security is rejecting the preflight.allowedOrigins and allowedOriginPatterns?allowedOrigins matches exact values. allowedOriginPatterns supports wildcards such as https://*.example.com and works together with allowCredentials.
CorsConfiguration needed if I use @CrossOrigin?No. @CrossOrigin builds a CorsConfiguration for you. Use the class directly when you need one reusable bean or fine-grained path mapping.
Most often the request is rejected earlier, by Spring Security or a gateway, so the response has no CORS headers. Enable http.cors(...) and check the preflight response.
No. Only browsers enforce CORS. Protect the API with authentication and authorisation.
CorsConfiguration stores the allowed origins, methods, headers, credentials setting and cache lifetime for CORS. Register it through a CorsConfigurationSource bean, WebMvcConfigurer or @CrossOrigin, enable cors() in Spring Security, and never combine wildcard origins with credentials.