CorsConfigurationSource (org.springframework.web.cors.CorsConfigurationSource) is the Spring interface that decides which CORS rules apply to an incoming request. It has one method, getCorsConfiguration(HttpServletRequest), that returns a CorsConfiguration for the request, or null when no CORS rules apply. Spring's CorsFilter and Spring Security's CORS support both call it for every request.
In practice you declare a CorsConfigurationSource bean, usually backed by UrlBasedCorsConfigurationSource, so that one central place maps URL patterns to CORS policies. The examples target Spring Framework 6, Spring Security 6 and Spring Boot 3 or later.
CorsConfigurationSourceUrlBasedCorsConfigurationSourceCorsFilterCorsConfigurationSourceCorsConfiguration describes one policy (origins, methods, headers, credentials). CorsConfigurationSource answers a different question: given this request, which policy applies? That separation lets one application use a strict policy for /admin/**, a relaxed one for /public/** and none elsewhere.
The built-in implementations are:
| Implementation | Package | Used with |
|---|---|---|
UrlBasedCorsConfigurationSource | org.springframework.web.cors | Servlet applications, matching paths with patterns |
UrlBasedCorsConfigurationSource | org.springframework.web.cors.reactive | WebFlux applications |
You can also implement the interface yourself, which is helpful when allowed origins come from a database or tenant settings.
UrlBasedCorsConfigurationSourceRequests to /api/** accept only the named origin and may carry credentials. Requests to /public/** accept any origin but only GET. A path that matches neither pattern gets no CORS headers, so browsers block cross-origin access.
The security filter chain runs before Spring MVC, so a preflight OPTIONS request that is not authenticated would be rejected before MVC could answer it. Enable CORS in the chain, and Spring Security uses your CorsConfigurationSource bean.
Customizer.withDefaults() picks up the corsConfigurationSource bean. To point at a different source, pass it explicitly with http.cors(c -> c.configurationSource(mySource)). Disabling CSRF here is only appropriate for stateless token-based APIs, not for browser sessions with cookies.
CorsFilterWithout Spring Security, you can register a CorsFilter that wraps the same source.
For WebFlux, the equivalent is CorsWebFilter from org.springframework.web.cors.reactive, built with the reactive UrlBasedCorsConfigurationSource.
Because the interface has one method, a custom implementation can choose rules per request.
Keep any lookup fast or cached, since it runs on every request, including preflights.
CorsConfiguration and CorsConfigurationSource?CorsConfiguration holds one set of rules. CorsConfigurationSource returns the right CorsConfiguration for a given request.
CorsConfigurationSource bean with @CrossOrigin?No. @CrossOrigin and WebMvcConfigurer.addCorsMappings use Spring MVC's own mapping. Define the bean when you use Spring Security's cors() or a CorsFilter.
corsConfigurationSource matter?Spring Security looks for a bean of this type with that name when you call cors() with defaults, so the default name is the safest choice.
* for origins with credentials?No. Use explicit origins or allowedOriginPatterns, which Spring accepts together with allowCredentials.
CorsConfigurationSource maps each request to a CorsConfiguration. Declare it as a bean with UrlBasedCorsConfigurationSource, enable cors() in Spring Security or register a CorsFilter, and implement the interface yourself when rules depend on the request.