79091352

Date: 2024-10-15 18:52:05
Score: 0.5
Natty:
Report link
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("/**")
                .allowedOrigins("http://localhost:4200")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .exposedHeaders("Authorization")
                .allowCredentials(true)
                .maxAge(3600L);
    }
}

Make sure to remove any conflicting CORS annotations from your controller. The @CrossOrigin annotation is generally not necessary if you’re handling CORS globally.Since you are already using the CORS configuration, you should remove the manual header setting in your interceptor

httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,DELETE,PUT");
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @CrossOrigin
  • Low reputation (1):
Posted by: Manish Keshari