My SpringBoot Application Code :
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://localhost:3000");
registry.addMapping("/**").allowedOrigins();
}
};
}
}
I have also implemented Controller Method CORS configuration as mentioned in https://spring.io/guides/gs/rest-service-cors/
I am using this code snippet to add a response header to enable CORS after referring this.
@ModelAttribute
public void setResponseHeader(HttpServletResponse response) {
response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
}
According to this answer, I also tried adding these headers in my frontend in axios interceptors.
I have read a lot of answers on StackOverflow about the same issue, titled "Access-Control-Allow-Origin" but still coudnt figure out how to solve this. I dont want to use an extension IN Chrome or any proxy or use a temporary hack to solve this. Is there any way I can fix this?