My API worked perfectly in Postman. Every endpoint returned clean JSON. Then I pointed my React app at it, and the browser refused to touch it. If that sequence sounds familiar, you have met CORS, the most searched full-stack beginner error, and the fix takes ten minutes once you understand what the browser is doing.

Key takeaways

  • CORS is a browser security rule, not a backend bug: an API that works in Postman but fails from React with "blocked by CORS policy" almost always needs server-side CORS configuration.
  • Configure allowed origins once with a global WebMvcConfigurer class instead of sprinkling @CrossOrigin on every controller.
  • When Spring Security is present, the security filter chain rejects the OPTIONS preflight before CORS rules apply: wire CORS in with http.cors() plus a CorsConfigurationSource bean.
  • Never combine allowedOrigins("*") with allowCredentials(true); browsers reject that combination outright.
  • For local development only, a React dev-server proxy ("proxy": "http://localhost:8080" in package.json) sidesteps CORS; it does not exist in production.

What CORS actually is

CORS stands for Cross-Origin Resource Sharing, and it is a browser security rule, not a bug. An "origin" is the combination of protocol, host, and port. Your React app on http://localhost:3000 and your Spring Boot API on http://localhost:8080 are two different origins, so the browser blocks the response unless the server explicitly allows it. The server still receives the request and processes it; the browser just throws the response away. That is why the error feels so strange: the backend did its job, and the frontend never sees the result.

The signature symptom: works in Postman, fails in React

Postman is not a browser, so it never enforces CORS. If an endpoint works in Postman but fails from React with "has been blocked by CORS policy" or a failed preflight, the backend is fine and the wiring between the two is not. For certain requests, the browser first sends a preflight OPTIONS request asking for permission; only when the server answers correctly does the real request go out. Any fix that ignores the preflight is incomplete.

The proper fix: configure CORS on the server

The clean fix is to tell Spring Boot which origins are allowed. A global configuration class keeps it in one place instead of sprinkling @CrossOrigin on every controller:

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")
                .allowedOrigins("http://localhost:3000")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("Authorization", "Content-Type")
                .allowCredentials(true)
                .maxAge(3600);
    }
}

Use your real frontend URL in production instead of localhost. If you use @CrossOrigin on a single controller for a quick test, that is fine, but the global config is the version that survives a real project.

The trap: Spring Security eats your preflight

This is where most beginners get stuck. The CORS config above does nothing if Spring Security is in the picture, because the security filter chain rejects the OPTIONS preflight before it ever reaches the CORS handler. The result is a 401 or 403 on the preflight, and the browser reports a CORS error even though CORS was configured. Wire CORS into the security chain explicitly:

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http
        .cors(Customizer.withDefaults())
        .csrf(csrf -> csrf.disable())
        .authorizeHttpRequests(auth -> auth
            .requestMatchers("/api/auth/**").permitAll()
            .anyRequest().authenticated());
    return http.build();
}

@Bean
public CorsConfigurationSource corsConfigurationSource() {
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowedOrigins(List.of("http://localhost:3000"));
    config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
    config.setAllowedHeaders(List.of("Authorization", "Content-Type"));
    config.setAllowCredentials(true);
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", config);
    return source;
}

The two halves belong together: http.cors() tells security to consult CORS rules, and the CorsConfigurationSource bean supplies those rules. Miss the first and the preflight dies at the security gate.

The dev shortcut: a React proxy

When you are only developing locally, you can dodge CORS entirely. Add this to your package.json in a Create React App project:

"proxy": "http://localhost:8080"

Then call the API with relative paths, like fetch("/api/rooms"). The dev server forwards those requests to Spring Boot, so the browser sees a same-origin call and CORS never applies. Treat it as a development shortcut only; in production, configure CORS properly on the backend, because the proxy does not exist there.

Two rules that save you later

First, never combine allowedOrigins("*") with allowCredentials(true). Browsers reject that combination outright, and Spring Boot will fail rather than guess what you meant. List your exact origins when credentials are involved. Second, if your React app sends an Authorization header or cookies, the preflight only passes when those headers are in your allowed list. Forgetting Authorization in allowedHeaders is a classic: every login request dies while every unauthenticated GET works, which looks like an auth bug but is really a CORS bug.

A quick debugging checklist

When CORS strikes again, walk through this list: open the browser's Network tab and find the OPTIONS request, its status tells you whether the preflight failed (401 or 403 usually means Spring Security is blocking it) or succeeded. Check the exact origin of your frontend, including the port, against your allowed origins. Restart the backend after changing CORS config, Spring does not pick it up from a stale run. And confirm the request works in Postman first; if Postman fails too, the problem is not CORS at all.