Handling CORS with Spring Boot REST API and React - Preflight Request Issues
I'm upgrading from an older version and I'm relatively new to this, so bear with me. I've searched everywhere and can't find a clear answer. I'm working on a Spring Boot REST API (version 2.5.6) that serves a React frontend (version 17.0.2). I have set up CORS configuration in my Spring Boot application, but I'm working with issues with preflight requests. When I attempt to make a POST request from my React app to the API, the browser throws an behavior: ``` Access to fetch at 'http://localhost:8080/api/resource' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP status code 200. ``` I've already defined a CORS configuration class like this: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**").allowedOrigins("http://localhost:3000").allowedMethods("GET", "POST", "PUT", "DELETE").allowedHeaders("*").allowCredentials(true); } } ``` I've also tried using `@CrossOrigin` directly on the controller methods, but I still see the same behavior. I'm not sure if there's something wrong with my configurations or if I'm missing something regarding the preflight requests. I've tested the same API endpoint using Postman, and it works without CORS issues. Can someone guide to figure out what might be going wrong with the CORS setup for my Spring Boot API and how to handle these preflight requests correctly? Any help would be greatly appreciated! Am I missing something obvious? Any ideas what could be causing this? I've been using Java for about a year now. Thanks for your help in advance!