Browse Source

fix 修改跨域

Administrator 4 years ago
parent
commit
1089264982
1 changed files with 20 additions and 2 deletions
  1. 20 2
      boman-gateway/src/main/java/com/boman/gateway/config/CrosConfig.java

+ 20 - 2
boman-gateway/src/main/java/com/boman/gateway/config/CrosConfig.java

@@ -77,6 +77,7 @@ import org.springframework.web.server.ServerWebExchange;
 import org.springframework.web.server.WebFilter;
 import org.springframework.web.server.WebFilterChain;
 import reactor.core.publisher.Mono;
+import static com.boman.common.core.utils.obj.ObjectUtils.*;
 
 /** 跨域请求
  * @author shiqian
@@ -92,10 +93,27 @@ public class CrosConfig {
             if (CorsUtils.isCorsRequest(request)) {
                 ServerHttpResponse response = ctx.getResponse();
                 HttpHeaders headers = response.getHeaders();
-                headers.add("Access-Control-Allow-Origin", "*");
+
+                if(isEmpty(headers.get("Access-Control-Allow-Credentials"))) {
+                    // 设置允许Cookie
+                    headers.add("Access-Control-Allow-Credentials", "true");
+                }
+                if(isEmpty(headers.get("Access-Control-Allow-Origin"))) {
+                    // 允许http://www.xxx.com域(自行设置,这里只做示例)发起跨域请求
+                    headers.add("Access-Control-Allow-Origin", "*");
+                }
+                if(isEmpty(headers.get("Access-Control-Allow-Methods"))) {
+                    // 设置允许跨域请求的方法
+                    headers.add("Access-Control-Allow-Methods", "*");
+                }
+                if(isEmpty(headers.get("Access-Control-Allow-Headers"))) {
+                    // 设置允许跨域请求的方法
+                    headers.add("Access-Control-Allow-Headers", "*");
+                }
+/*                headers.add("Access-Control-Allow-Origin", "*");
                 headers.add("Access-Control-Allow-Methods", "*");
                 headers.add("Access-Control-Allow-Headers", "*");
-                headers.add("Access-Control-Allow-Credentials", "true");
+                headers.add("Access-Control-Allow-Credentials", "true");*/
                 if (request.getMethod() == HttpMethod.OPTIONS) {
                     response.setStatusCode(HttpStatus.OK);
                     return Mono.empty();