chenshudong пре 4 месеци
родитељ
комит
b65de8b1c2

+ 18 - 3
airport-framework/src/main/java/com/sundot/airport/framework/config/ServerConfig.java

@@ -2,6 +2,7 @@ package com.sundot.airport.framework.config;
2 2
 
3 3
 import javax.servlet.http.HttpServletRequest;
4 4
 
5
+import cn.hutool.core.util.StrUtil;
5 6
 import org.springframework.stereotype.Component;
6 7
 import com.sundot.airport.common.utils.ServletUtils;
7 8
 
@@ -24,10 +25,24 @@ public class ServerConfig {
24 25
 
25 26
     public static String getDomain(HttpServletRequest request) {
26 27
         StringBuffer url = request.getRequestURL();
27
-        String contextPath = request.getContextPath();
28
-        // 删除请求URI部分,保留基础URL
28
+        String contextPath = request.getServletContext().getContextPath();
29 29
         url.delete(url.length() - request.getRequestURI().length(), url.length());
30
-        // 直接返回基础URL + 上下文路径,完全保留原始端口
30
+
31
+        // 如果 URL 以 / 结尾,去掉它(避免出现 http://example.com/:9011)
32
+        if (url.charAt(url.length() - 1) == '/') {
33
+            url.deleteCharAt(url.length() - 1);
34
+        }
35
+        // 获取当前请求的端口
36
+        int currentPort = request.getServerPort();
37
+        // 如果是80或443端口(HTTP/HTTPS默认端口),则强制使用9011端口
38
+        if (!StrUtil.equals("http://192.168.3.221", url) && (currentPort == 80 || currentPort == 443)) {
39
+            int protocolEnd = url.indexOf("://") + 3;
40
+            int portStart = url.indexOf(":", protocolEnd);
41
+            if (portStart < 0) {
42
+                // 没有端口,添加端口
43
+                url.insert(url.length(), ":9021");
44
+            }
45
+        }
31 46
         return url.append(contextPath).toString();
32 47
     }
33 48
 }