CorsFilter is a Spring Web class (org.springframework.web.filter.CorsFilter) that applies Cross-Origin Resource Sharing rules to incoming requests. It answers preflight OPTIONS requests and adds the Access-Control-* response headers, based on a CorsConfigurationSource you supply. Because it is a servlet filter, it runs before Spring MVC and before Spring Security filters when ordered correctly.
You need CORS when a browser page served from one origin calls an API on another origin, for example a frontend on https://app.example.com calling https://api.example.com.
Browsers block cross-origin responses unless the server opts in with headers. For non-simple requests (custom headers, methods such as PUT or DELETE, or JSON bodies) the browser first sends a preflight OPTIONS request with Origin, Access-Control-Request-Method and Access-Control-Request-Headers. The server must reply with matching Access-Control-Allow-* headers, or the browser refuses the real request.
Spring offers several ways to provide those headers:
| Approach | Level | Use when |
|---|---|---|
@CrossOrigin | Controller or method | A few endpoints need special rules |
WebMvcConfigurer.addCorsMappings | Spring MVC | Global rules, no Spring Security |
http.cors(...) in Spring Security | Security filter chain | Spring Security is present (recommended) |
CorsFilter bean | Servlet filter | You want the policy applied in the filter chain, including for non-MVC paths |
CorsFilter and Spring Security's CorsConfigurer both use CorsConfigurationSource and CorsConfiguration underneath.
In Spring Boot, a Filter bean is registered automatically for all URLs. The configuration applies only to paths that match /api/** in the source.
What each setting does:
allowedOrigins lists exact origins. Use setAllowedOriginPatterns for patterns such as https://*.example.com.allowCredentials(true) lets browsers send cookies and Authorization headers; it cannot be combined with a wildcard * origin, so list origins or use patterns.exposedHeaders lets JavaScript read response headers beyond the safe defaults.maxAge tells the browser how long to cache the preflight result.When Spring Security is on the classpath, its filters can reject a preflight request (which carries no credentials) with 401 before the MVC layer sees it. The current recommended approach is to configure CORS in the security chain so it runs first:
With Customizer.withDefaults(), Spring Security looks for a bean named corsConfigurationSource and adds a CorsFilter to its chain at the correct position. Do not also register a separate CorsFilter bean for the same paths, or headers may be applied twice.
Without Spring Security, a WebMvcConfigurer is the most direct choice:
For a single controller, annotate it with @CrossOrigin(origins = "https://app.example.com").
* for origins together with credentials. Browsers reject it, and Spring throws an exception for it.curl or Postman will never show a CORS error.http.cors(...).Authorization in the allowed headers.Access-Control-Allow-Origin: a, a.To debug, open the browser network tab, find the failed OPTIONS call and compare its request headers with the response Access-Control-Allow-* headers.
No. @CrossOrigin is an annotation processed by Spring MVC for handler methods. CorsFilter is a servlet filter that handles CORS earlier, for any path you configure.
Not as a separate bean. Use http.cors(...) with a CorsConfigurationSource bean, which installs a CorsFilter for you in the right place.
No. It only tells browsers which origins may read responses. Authentication and authorisation still have to be enforced on the server. CSRF protection is a separate concern.
Postman does not enforce CORS. The browser blocks the response because the server did not send the right Access-Control-Allow-Origin header for your origin.
CorsFilter applies a CorsConfiguration to requests in the servlet filter chain, answering preflight requests and adding the CORS headers. Use it directly for plain filter-based setups, prefer http.cors(...) when Spring Security is present, and list explicit origins when credentials are involved.