Merge pull request #13624 from dataease/pr@dev-v2@perf_cors_config

feat: 增加是否严格校验跨域配置
This commit is contained in:
xuwei-fit2cloud 2024-11-28 11:05:12 +08:00 committed by GitHub
commit 1186bb7f89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -15,6 +15,9 @@ import java.util.List;
@Configuration @Configuration
public class CorsConfig implements WebMvcConfigurer { public class CorsConfig implements WebMvcConfigurer {
@Value("${dataease.cors-strict:false}")
private boolean corsStrict;
@Value("#{'${dataease.origin-list:http://127.0.0.1:8100}'.split(',')}") @Value("#{'${dataease.origin-list:http://127.0.0.1:8100}'.split(',')}")
private List<String> originList; private List<String> originList;
@ -29,15 +32,19 @@ public class CorsConfig implements WebMvcConfigurer {
@Override @Override
public void addCorsMappings(CorsRegistry registry) { public void addCorsMappings(CorsRegistry registry) {
operateCorsRegistration = registry.addMapping("/**") operateCorsRegistration = registry.addMapping("/**")
.allowCredentials(true) .allowCredentials(false)
.allowedOrigins(originList.toArray(new String[0]))
.allowedHeaders("*") .allowedHeaders("*")
.maxAge(3600) .maxAge(3600)
.allowedMethods("GET", "POST", "DELETE"); .allowedMethods("GET", "POST", "DELETE");
if (corsStrict) {
operateCorsRegistration.allowedOrigins(originList.toArray(new String[0]));
return;
}
operateCorsRegistration.allowedOrigins("*");
} }
public void addAllowedOrigins(List<String> origins) { public void addAllowedOrigins(List<String> origins) {
if (CollectionUtils.isEmpty(origins)) { if (!corsStrict || CollectionUtils.isEmpty(origins)) {
return; return;
} }
origins.addAll(originList); origins.addAll(originList);