Spring returns 403 with the body 'Invalid CORS request'
Invalid CORS requestShort answer
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. Compare the request's Origin and Access-Control-Request-* headers with your CORS rules. With Spring Security, enable CORS there as well.
Fix it fast
1Find out which cause you have
Replay the failing browser request with curl, using the exact Origin, method and headers from DevTools. A 403 with the body Invalid CORS request means Spring rejected it. Then change one value at a time to see which check fails.
curl -i -X OPTIONS http://localhost:8080/api/items \ -H "Origin: http://localhost:5173" \ -H "Access-Control-Request-Method: PUT" \ -H "Access-Control-Request-Headers: content-type"2Match what you see, then jump to its fix
- 1
403 Invalid CORS request, and the Origin you sent differs from the configured one by scheme, host or port.The origin is not in the allowed listFix - 2
GET and POST work, but PUT, PATCH or DELETE fail with 403 Invalid CORS request on the preflight.The method is not allowed: PUT and DELETE are missing from the global defaultsFix - 3
The app was set to allowedOrigins("*") with allowCredentials(true), and cookie-based calls are rejected or the application fails at start-up.Credentials are enabled together with a wildcard originFix - 4
The OPTIONS request returns 401 or 403 without any Access-Control headers, while MVC CORS mappings look correct.Spring Security rejects the preflight before MVC sees itFix
- 1
What does the error look like?
Invalid CORS request403 Forbidden on the OPTIONS requestAccess to fetch at 'http://localhost:8080/api/items' from origin 'http://localhost:5173' has been blocked by CORS policy
Cause 1 of 4
The origin is not in the allowed list
1Check
Spring allows cross-origin requests only for origins you declared. If no matching CORS configuration exists, it adds no CORS headers at all, and browsers then reject the call. Compare your configured origin with the Origin header character by character.
grep -rn "allowedOrigins\|@CrossOrigin" src/main2Fix
Declare the real origin, including the port, in a global configuration.
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("http://localhost:5173", "https://app.example.com");
}
}3Verify
Repeat the preflight: it should return 200 with Access-Control-Allow-Origin.
curl -i -X OPTIONS http://localhost:8080/api/items \
-H "Origin: http://localhost:5173" -H "Access-Control-Request-Method: GET"Confirmed by docs.spring.io
Cause 2 of 4
The method is not allowed: PUT and DELETE are missing from the global defaults
1Check
A global CORS mapping allows GET, HEAD and POST by default. Anything else must be listed explicitly. (The @CrossOrigin annotation instead allows the methods the handler is mapped to.)
curl -i -X OPTIONS http://localhost:8080/api/items -H "Origin: http://localhost:5173" -H "Access-Control-Request-Method: DELETE"2Fix
List the methods the frontend uses.
registry.addMapping("/api/**")
.allowedOrigins("http://localhost:5173")
.allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE");3Verify
The DELETE preflight should now answer 200 and list the method in Access-Control-Allow-Methods.
curl -i -X OPTIONS http://localhost:8080/api/items -H "Origin: http://localhost:5173" -H "Access-Control-Request-Method: DELETE"Confirmed by docs.spring.io
Cause 3 of 4
Credentials are enabled together with a wildcard origin
1Check
With credentials on, Spring does not accept a wildcard in allowedOrigins. Use explicit origins, or allowedOriginPatterns for a dynamic set.
grep -rn "allowCredentials\|allowedOrigins(\"\*\")" src/main2Fix
Replace the wildcard with explicit origins or a pattern.
registry.addMapping("/api/**")
.allowedOriginPatterns("https://*.example.com")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowCredentials(true);3Verify
The preflight returns Access-Control-Allow-Credentials: true and your origin, not *.
curl -i -X OPTIONS http://localhost:8080/api/items \
-H "Origin: https://shop.example.com" -H "Access-Control-Request-Method: POST"Confirmed by docs.spring.io
Cause 4 of 4
Spring Security rejects the preflight before MVC sees it
1Check
A preflight carries no cookies. If Spring Security runs first it treats the request as unauthenticated and rejects it. CORS therefore has to be processed inside the security filter chain.
grep -rn "SecurityFilterChain\|\.cors(" src/main2Fix
Turn on CORS support in the security configuration. With Spring MVC on the classpath and no CorsConfigurationSource bean, Security reuses your MVC CORS configuration.
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.cors(Customizer.withDefaults())
.authorizeHttpRequests(auth -> auth.anyRequest().authenticated());
return http.build();
}3Verify
Send the preflight without any credentials; it should no longer return 401 or 403.
curl -i -X OPTIONS http://localhost:8080/api/items \
-H "Origin: http://localhost:5173" -H "Access-Control-Request-Method: GET"Confirmed by docs.spring.io
How do I stop it coming back?
- Keep the allowed origins in one configuration property and reuse it for both MVC and Security.
- Never disable CORS in Spring Security to silence the error; it removes the support instead of the browser rule.
- Add a preflight curl check for your main routes to the deploy tests.
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
Web 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
Apache HttpClient 5 has no setSSLSocketFactory on HttpClients.custom()
HttpClients.custom().setSSLSocketFactory() method not found
In Apache HttpClient 5 the TLS setup no longer sits on the client builder.
2 causes, each with a fixBack end
A scanner flags jackson-databind: how to upgrade it in Gradle
jackson-databind vulnerability reported by a dependency scanner
Find where the old jackson-databind comes from, then raise it.
3 causes, each with a fixBack end
Spring Boot cannot bind spring.datasource.password with Jasypt ENC() values
Failed to bind properties under 'spring.datasource.password' to java.lang.String
The binding error means Jasypt could not turn the ENC(...) value back into plain text.
3 causes, each with a fixBack end
Gradle cannot resolve spring-boot-gradle-plugin after a Spring Boot 3 upgrade
Plugin [id: 'org.springframework.boot', version: '3.0.1'] was not found in any of the following sources
Spring Boot 3 needs Java 17 or newer and a recent Gradle, and its Gradle plugin is published on the Gradle Plugin Portal.
3 causes, each with a fixBack end
Spring Boot global CORS configuration has no effect, but @CrossOrigin works
The preflight OPTIONS request has no Access-Control headers, and a controller annotated with @CrossOrigin on the same app answers correctly
Spring checks CORS only after it maps a request to a handler, and only against a configuration that matches that request.
6 causes, each with a fix