1 Answer
- Newest
- Most votes
- Most comments
0
your issue might be related to CORS (Cross-Origin Resource Sharing), which is a security feature implemented in web browsers to prevent web pages from making requests to a different domain than the one that served the web page
ince your React app and Java Spring Boot backend are running on different containers (and therefore have different domains), you'll need to configure CORS properly in your backend to allow your frontend to make requests.
You can enable CORS globally in your Spring Boot application by adding the following configuration to your main application class
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 {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // Apply CORS to all endpoints
.allowedOrigins("http://your-react-app-domain.com") // Replace with your React app's domain
.allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS") // Allowed HTTP methods
.allowCredentials(true) // Allow cookies to be sent with the request
.maxAge(3600); // Maximum time in seconds that the browser should cache the CORS policy
}
};
}
}
hope it works for you
Relevant content
- asked 16 days ago
- asked 8 months ago
- AWS OFFICIALUpdated 4 years ago
- AWS OFFICIALUpdated 3 months ago
thanks my dude, will give this a try now