Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Spring returns 403 with the body 'Invalid CORS request'

ErrorInvalid CORS request

Short 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.

  • Applies to Spring Framework 6, Spring Boot 3, Spring Security 6, Java 21
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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"
  2. 2Match what you see, then jump to its fix

    1. 1403 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. 2GET 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. 3The 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. 4The 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

What does the error look like?

  • Invalid CORS request
  • 403 Forbidden on the OPTIONS request
  • Access 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/main

2Fix

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

Did this fix it?

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

Did this fix it?

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/main

2Fix

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

Did this fix it?

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/main

2Fix

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

Did this fix it?

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.