I have this WebMvcConfigurer
which works totally fine when deployed over the server. But when I try to send a request from my locally served Angular project to the server, I get the following error.
Access to XMLHttpRequest at 'https://sub.domain.com/api/staff/logout' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
My Config is as follows:
@Configuration
@EnableWebMvc
public class WebMvcConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS").allowedOrigins("*")
.allowedHeaders("*")
.exposedHeaders("Access-Control-Allow-Origin", "Access-Control-Allow-Credentials")
.allowCredentials(true).maxAge(3600);
}
/**
* To add our interceptor into Spring configuration, we need to override
* addInterceptors() method WebMvcConfig class that implements WebMvcConfigurer.
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AppInterceptor());
}
};
}
}
I have referred to other questions my didn't find anything that solved my problem. Thanks in advance.