CORS Issue Cont.

  • CORS Solution Cont.

In spring-boot project configuration,

@Configuration
@ConfigurationProperties(prefix = "app.corsSites")
public class CorsSiteConfiguration {

public List<String> getAllowSites() {
return allowSites;
}

List<String> allowSites = new ArrayList<String>();

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
CorsRegistration corsRegistration = registry.addMapping("/**")
//config in yml file
.allowedOrigins("http://10.0.0.1:8081") //demo
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowCredentials(false)
.maxAge(3600);
if (allowSites == null || allowSites.size() == 0) {
corsRegistration.allowedOrigins("*");
} else {
allowSites.forEach(site -> corsRegistration.allowedOrigins(site));
}
}
};
}
}

In yml file,

app:
cors-sites:
allow-sites:
- "*"