Explorar o código

CORS跨域: request.getHeader("Origin")=null時设置前端的地址

csg6 hai 1 semana
pai
achega
c8c4fcb024

+ 6 - 6
ruoyi-admin/src/main/resources/application-druid-dev.yml

@@ -31,14 +31,14 @@ spring:
31 31
         druid:
32 32
             # 主库数据源
33 33
             master:
34
-#                url: jdbc:kingbase8://localhost:54321/postcheck
34
+                url: jdbc:kingbase8://localhost:54321/postcheck
35 35
 #                url: jdbc:kingbase8://39.106.66.72:54321/postcheck
36
-#                username: postcheckuser
37
-#                password: Hzsh.eitc@6626.pcu
36
+                username: postcheckuser
37
+                password: Hzsh.eitc@6626.pcu
38 38
 
39
-                url: jdbc:kingbase8://192.168.3.242:54321/postcheck
40
-                username: system
41
-                password: 123456
39
+#                url: jdbc:kingbase8://192.168.3.242:54321/postcheck
40
+#                username: system
41
+#                password: 123456
42 42
             # 从库数据源
43 43
             slave:
44 44
                 # 从数据源开关/默认关闭

+ 8 - 3
ruoyi-common/src/main/java/com/ruoyi/common/filter/RestCorsFilter.java

@@ -1,5 +1,6 @@
1 1
 package com.ruoyi.common.filter;
2 2
 
3
+import com.ruoyi.common.utils.StringUtils;
3 4
 import org.springframework.core.Ordered;
4 5
 import org.springframework.core.annotation.Order;
5 6
 import org.springframework.stereotype.Component;
@@ -28,13 +29,17 @@ public class RestCorsFilter implements Filter {
28 29
         String[] allowDomain = {
29 30
                 "http://10.152.70.21:8080", // CAS服务器
30 31
                 "http://10.152.72.7:8181" // 岗检前端Nginx
31
-               // "*.cnooc.*"
32
+                // "*.cnooc.*"
32 33
                 // TODO 打包屏蔽-甲方部署时注释:甲方不需要本地登录功能采用单点登录
33
-//                ,"http://192.168.3.32:81"  // 前端开发
34
+                , "http://192.168.3.32:81"  // 前端开发
34 35
         };
35 36
         Set<String> allowedOrigins = new HashSet<>(Arrays.asList(allowDomain));
36 37
         String originHeader = request.getHeader("Origin");
37 38
 
39
+        if (StringUtils.isBlank(originHeader)) {
40
+            originHeader = "http://192.168.3.32:81";
41
+        }
42
+
38 43
         if (allowedOrigins.contains(originHeader)) {
39 44
             response.setHeader("Access-Control-Allow-Origin", originHeader);
40 45
             response.setHeader("Access-Control-Allow-Credentials", "true");
@@ -46,7 +51,7 @@ public class RestCorsFilter implements Filter {
46 51
             response.setStatus(HttpServletResponse.SC_FORBIDDEN);
47 52
             return;
48 53
         }
49
-            chain.doFilter(req, res);
54
+        chain.doFilter(req, res);
50 55
     }
51 56
 
52 57
     @Override