|
|
@@ -3,28 +3,48 @@ package com.fs.common.config;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.cors.CorsConfiguration;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
+import java.util.LinkedHashSet;
|
|
|
import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
/**
|
|
|
* CORS 白名单(Spring Boot 2.2 无 setAllowedOriginPatterns,使用显式 Origin 列表)。
|
|
|
* 通过环境变量 CORS_ALLOWED_ORIGINS 配置,逗号分隔;未配置时使用内置业务域名。
|
|
|
+ * <p>
|
|
|
+ * 注意:浏览器访问 http://localhost/ 时 Origin 为 {@code http://localhost}(无端口),
|
|
|
+ * 与 {@code http://localhost:80} 不同;漏配会导致 CorsFilter 直接 403。
|
|
|
*/
|
|
|
public final class CorsSupport {
|
|
|
|
|
|
private static final List<String> DEFAULT_ORIGINS = Arrays.asList(
|
|
|
+ // 金牛明医
|
|
|
+ "https://admin.jnmyunl.com",
|
|
|
+ "https://company.jnmyunl.com",
|
|
|
+ "https://doctor.jnmyunl.com",
|
|
|
+ // 通用业务域
|
|
|
"https://admin.cdwjyyh.com",
|
|
|
"https://company.cdwjyyh.com",
|
|
|
"https://doctor.cdwjyyh.com",
|
|
|
"https://h5.cdwjyyh.com",
|
|
|
+ // 本地:无端口(默认 80/443)与常见开发端口
|
|
|
+ "http://localhost",
|
|
|
+ "http://127.0.0.1",
|
|
|
"http://localhost:80",
|
|
|
+ "http://localhost:81",
|
|
|
+ "http://localhost:1024",
|
|
|
"http://localhost:8080",
|
|
|
"http://localhost:8081",
|
|
|
- "http://localhost:1024",
|
|
|
+ "http://localhost:9527",
|
|
|
+ "http://localhost:9528",
|
|
|
"http://127.0.0.1:80",
|
|
|
+ "http://127.0.0.1:81",
|
|
|
+ "http://127.0.0.1:1024",
|
|
|
"http://127.0.0.1:8080",
|
|
|
"http://127.0.0.1:8081",
|
|
|
- "http://127.0.0.1:1024"
|
|
|
+ "http://127.0.0.1:9527",
|
|
|
+ "http://127.0.0.1:9528"
|
|
|
);
|
|
|
|
|
|
private CorsSupport() {
|
|
|
@@ -38,14 +58,20 @@ public final class CorsSupport {
|
|
|
}
|
|
|
config.addAllowedHeader("*");
|
|
|
config.addAllowedMethod("*");
|
|
|
+ config.setMaxAge(3600L);
|
|
|
return config;
|
|
|
}
|
|
|
|
|
|
public static List<String> resolveOrigins() {
|
|
|
+ Set<String> origins = new LinkedHashSet<>(DEFAULT_ORIGINS);
|
|
|
String env = System.getenv("CORS_ALLOWED_ORIGINS");
|
|
|
if (StringUtils.hasText(env)) {
|
|
|
- return Arrays.asList(env.split("\\s*,\\s*"));
|
|
|
+ for (String o : env.split("\\s*,\\s*")) {
|
|
|
+ if (StringUtils.hasText(o)) {
|
|
|
+ origins.add(o.trim());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- return DEFAULT_ORIGINS;
|
|
|
+ return new ArrayList<>(origins);
|
|
|
}
|
|
|
}
|