Spring Boot global CORS configuration has no effect, but @CrossOrigin works
No 'Access-Control-Allow-Origin' header is present on the requested resource.Short answer
Spring checks CORS only after it maps a request to a handler, and only against a configuration that matches that request. When a global setup does nothing, check that the WebMvcConfigurer is a registered bean, that the pattern covers the URL, that the URL really reaches a controller, and that the app is not WebFlux or an Actuator endpoint.
Fix it fast
1Find out which cause you have
Call the URL once as a plain request to see its real status, then again with an Origin header, the way a browser sends it. A 404 on the plain call means the problem is the URL. A 200 with no Access-Control headers on the second call means no CORS configuration matches the request.
curl -i http://localhost:8080/api/orders curl -i http://localhost:8080/api/orders \ -H "Origin: http://localhost:5173"2Match what you see, then jump to its fix
- 1
No response from the app carries CORS headers, and a log line or breakpoint inside addCorsMappings is never reachedThe WebMvcConfigurer is not registered as a beanFix - 2
CORS works for one path prefix, for example /api/orders, and not for another such as /v2/orders or /healthThe mapping pattern does not cover the URL being calledFix - 3
The plain request from the first step returns 404, and the browser still reports a CORS failureThe request never reaches a controller, so CORS is never evaluatedFix - 4
GET and POST pass, PUT, PATCH and DELETE fail at the preflight, and the same method works on a controller with @CrossOriginGlobal defaults allow only GET, HEAD and POSTFix - 5
The project depends on spring-boot-starter-webflux, and the CORS class was copied from a Spring MVC exampleThe application is reactive and needs the WebFlux configurationFix - 6
Your own /api calls work from the browser, but a call to /actuator/health or another Actuator endpoint is blockedThe blocked call goes to an Actuator endpointFix
- 1
What does the error look like?
No 'Access-Control-Allow-Origin' header is present on the requested resource.The preflight OPTIONS request has no Access-Control headers, and a controller annotated with @CrossOrigin on the same app answers correctlyCORS error in the browser console, while the Network tab shows 404 for the same URL
Cause 1 of 6
The WebMvcConfigurer is not registered as a bean
1Check
Spring Boot takes global CORS rules from a WebMvcConfigurer bean. A class that implements the interface but is not registered as a bean is never called. Search for the class and its annotations.
grep -rn -B3 "WebMvcConfigurer" src/main/java2Fix
Register it: annotate the class with @Configuration, or expose the configurer from a @Bean method. Keep @EnableWebMvc off unless you want to replace Boot's MVC defaults.
@Configuration(proxyBeanMethods = false)
public class CorsConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("http://localhost:5173");
}
};
}
}3Verify
Restart the app and repeat the second request from the first step; Access-Control-Allow-Origin should appear.
curl -i http://localhost:8080/api/orders -H "Origin: http://localhost:5173"Confirmed by docs.spring.io
Cause 2 of 6
The mapping pattern does not cover the URL being called
1Check
A global configuration is a set of URL patterns, and Spring applies a mapping only to requests whose path matches its pattern. Compare each addMapping pattern with the path in the browser's Network tab.
grep -rn "addMapping" src/main/java2Fix
Add a mapping for each prefix that browsers call, or use /** for everything.
registry.addMapping("/api/**").allowedOrigins("http://localhost:5173");
registry.addMapping("/v2/**").allowedOrigins("http://localhost:5173");3Verify
Request a URL from each prefix with an Origin header and look for the header.
curl -is http://localhost:8080/v2/orders -H "Origin: http://localhost:5173" | grep -i access-controlConfirmed by docs.spring.io
Cause 3 of 6
The request never reaches a controller, so CORS is never evaluated
1Check
Spring looks at the CORS configuration only after a request has been mapped to a handler. If no matching configuration is found, it adds no CORS headers, and the browser rejects the response. A mistyped path therefore looks like a CORS problem.
curl -i -X POST http://localhost:8080/api/orders -H "Content-Type: application/json" -d '{}'2Fix
Fix the URL or the mapping first. Make the path and HTTP method in the controller match what the front end calls.
@RestController
@RequestMapping("/api/orders")
class OrderController {
@PostMapping
Order create(@RequestBody OrderRequest body) { /* ... */ }
}3Verify
The plain request now returns a normal status, and the request with an Origin header carries the CORS headers.
curl -i -X POST http://localhost:8080/api/orders -H "Origin: http://localhost:5173" -H "Content-Type: application/json" -d '{}'Confirmed by docs.spring.io
Cause 4 of 6
Global defaults allow only GET, HEAD and POST
1Check
A global mapping allows all origins and headers, but only the GET, HEAD and POST methods unless you list more. @CrossOrigin instead allows the methods its handler is mapped to, which is why it seems to work.
curl -i -X OPTIONS http://localhost:8080/api/orders/7 -H "Origin: http://localhost:5173" -H "Access-Control-Request-Method: DELETE"2Fix
List the methods in the global mapping. Global and local rules are combined: origins add up, and the local value wins for allowCredentials and maxAge.
registry.addMapping("/api/**")
.allowedOrigins("http://localhost:5173")
.allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE");3Verify
The DELETE preflight now answers with the method in Access-Control-Allow-Methods.
curl -is -X OPTIONS http://localhost:8080/api/orders/7 -H "Origin: http://localhost:5173" -H "Access-Control-Request-Method: DELETE" | grep -i access-controlConfirmed by docs.spring.io
Cause 5 of 6
The application is reactive and needs the WebFlux configuration
1Check
Spring WebFlux has its own configuration interface and its own filter. A WebMvcConfigurer written for Spring MVC is the wrong API for it. Check which starter the project uses.
grep -n "starter-web\|starter-webflux" pom.xml build.gradle build.gradle.kts 2>/dev/null2Fix
Implement WebFluxConfigurer, or declare a CorsWebFilter bean.
@Configuration
public class WebFluxCors implements WebFluxConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("http://localhost:5173")
.allowedMethods("GET", "POST", "PUT", "DELETE");
}
}3Verify
Request a reactive endpoint with an Origin header; the CORS headers should be in the response.
curl -is http://localhost:8080/api/orders -H "Origin: http://localhost:5173" | grep -i access-controlConfirmed by docs.spring.io
Cause 6 of 6
The blocked call goes to an Actuator endpoint
1Check
Actuator's web endpoints have their own CORS switch, and it is off by default. It turns on only when management.endpoints.web.cors.allowed-origins is set.
grep -rn "management.endpoints.web.cors" src/main/resources2Fix
Set the origins, and the methods you need, in the Actuator properties.
management:
endpoints:
web:
cors:
allowed-origins: "http://localhost:5173"
allowed-methods: "GET,POST"3Verify
Call an Actuator URL with an Origin header and read the response headers.
curl -is http://localhost:8080/actuator/health -H "Origin: http://localhost:5173" | grep -i access-controlConfirmed by docs.spring.io
How do I stop it coming back?
- Keep one CORS class for the application, and log or test that it loads at start-up.
- Choose either global mapping or @CrossOrigin as the main style, so the combined result is easy to predict.
- Always check the plain status of a URL before blaming CORS: a 404 or 500 shows up as a CORS error in the console.
- Keep the allowed origins in configuration properties, one set per environment.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Spring returns 403 with the body 'Invalid CORS request'
403 Forbidden on the OPTIONS request
Spring MVC answers 403 with the text Invalid CORS request when a cross-origin request fails one of its CORS checks: the origin, the method or a header is not allowed by your configuration.
4 causes, each with a fixWeb apps
The browser blocks your API response: no Access-Control-Allow-Origin header
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
The browser received the response but refused to hand it to your script, because it did not carry an Access-Control-Allow-Origin header naming the page's origin.
4 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ASP.NET Core has CORS set up but the browser still blocks the request
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
ASP.NET Core only adds CORS headers when a policy matches the request.
6 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fix