Przeglądaj źródła

优化IP地址获取到多个的问题

RuoYi 4 lat temu
rodzic
commit
e5c938c64a

+ 88 - 33
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ip/IpUtils.java

@@ -12,58 +12,62 @@ import com.ruoyi.common.core.utils.StringUtils;
12
  */
12
  */
13
 public class IpUtils
13
 public class IpUtils
14
 {
14
 {
15
+    /**
16
+     * 获取客户端IP
17
+     * 
18
+     * @param request 请求对象
19
+     * @return IP地址
20
+     */
15
     public static String getIpAddr(HttpServletRequest request)
21
     public static String getIpAddr(HttpServletRequest request)
16
     {
22
     {
17
         if (request == null)
23
         if (request == null)
18
         {
24
         {
19
-            return null;
25
+            return "unknown";
20
         }
26
         }
21
-
22
-        String ip = null;
23
-
24
-        // X-Forwarded-For:Squid 服务代理
25
-        String ipAddresses = request.getHeader("X-Forwarded-For");
26
-        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses))
27
+        String ip = request.getHeader("x-forwarded-for");
28
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
27
         {
29
         {
28
-            // Proxy-Client-IP:apache 服务代理
29
-            ipAddresses = request.getHeader("Proxy-Client-IP");
30
+            ip = request.getHeader("Proxy-Client-IP");
30
         }
31
         }
31
-        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses))
32
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
32
         {
33
         {
33
-            // WL-Proxy-Client-IP:weblogic 服务代理
34
-            ipAddresses = request.getHeader("WL-Proxy-Client-IP");
34
+            ip = request.getHeader("X-Forwarded-For");
35
         }
35
         }
36
-        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses))
36
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
37
         {
37
         {
38
-            // HTTP_CLIENT_IP:有些代理服务器
39
-            ipAddresses = request.getHeader("HTTP_CLIENT_IP");
38
+            ip = request.getHeader("WL-Proxy-Client-IP");
40
         }
39
         }
41
-        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses))
40
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
42
         {
41
         {
43
-            // X-Real-IP:nginx服务代理
44
-            ipAddresses = request.getHeader("X-Real-IP");
42
+            ip = request.getHeader("X-Real-IP");
45
         }
43
         }
46
 
44
 
47
-        // 有些网络通过多层代理,那么获取到的ip就会有多个,一般都是通过逗号(,)分割开来,并且第一个ip为客户端的真实IP
48
-        if (ipAddresses != null && ipAddresses.length() != 0)
49
-        {
50
-            ip = ipAddresses.split(",")[0];
51
-        }
52
-
53
-        // 还是不能获取到,最后再通过request.getRemoteAddr();获取
54
-        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses))
45
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
55
         {
46
         {
56
             ip = request.getRemoteAddr();
47
             ip = request.getRemoteAddr();
57
         }
48
         }
58
-        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
49
+
50
+        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : getMultistageReverseProxyIp(ip);
59
     }
51
     }
60
 
52
 
53
+    /**
54
+     * 检查是否为内部IP地址
55
+     * 
56
+     * @param ip IP地址
57
+     * @return 结果
58
+     */
61
     public static boolean internalIp(String ip)
59
     public static boolean internalIp(String ip)
62
     {
60
     {
63
         byte[] addr = textToNumericFormatV4(ip);
61
         byte[] addr = textToNumericFormatV4(ip);
64
         return internalIp(addr) || "127.0.0.1".equals(ip);
62
         return internalIp(addr) || "127.0.0.1".equals(ip);
65
     }
63
     }
66
 
64
 
65
+    /**
66
+     * 检查是否为内部IP地址
67
+     * 
68
+     * @param addr byte地址
69
+     * @return 结果
70
+     */
67
     private static boolean internalIp(byte[] addr)
71
     private static boolean internalIp(byte[] addr)
68
     {
72
     {
69
         if (StringUtils.isNull(addr) || addr.length < 2)
73
         if (StringUtils.isNull(addr) || addr.length < 2)
@@ -124,7 +128,8 @@ public class IpUtils
124
             {
128
             {
125
                 case 1:
129
                 case 1:
126
                     l = Long.parseLong(elements[0]);
130
                     l = Long.parseLong(elements[0]);
127
-                    if ((l < 0L) || (l > 4294967295L)){
131
+                    if ((l < 0L) || (l > 4294967295L))
132
+                    {
128
                         return null;
133
                         return null;
129
                     }
134
                     }
130
                     bytes[0] = (byte) (int) (l >> 24 & 0xFF);
135
                     bytes[0] = (byte) (int) (l >> 24 & 0xFF);
@@ -134,12 +139,14 @@ public class IpUtils
134
                     break;
139
                     break;
135
                 case 2:
140
                 case 2:
136
                     l = Integer.parseInt(elements[0]);
141
                     l = Integer.parseInt(elements[0]);
137
-                    if ((l < 0L) || (l > 255L)) {
142
+                    if ((l < 0L) || (l > 255L))
143
+                    {
138
                         return null;
144
                         return null;
139
                     }
145
                     }
140
                     bytes[0] = (byte) (int) (l & 0xFF);
146
                     bytes[0] = (byte) (int) (l & 0xFF);
141
                     l = Integer.parseInt(elements[1]);
147
                     l = Integer.parseInt(elements[1]);
142
-                    if ((l < 0L) || (l > 16777215L)) {
148
+                    if ((l < 0L) || (l > 16777215L))
149
+                    {
143
                         return null;
150
                         return null;
144
                     }
151
                     }
145
                     bytes[1] = (byte) (int) (l >> 16 & 0xFF);
152
                     bytes[1] = (byte) (int) (l >> 16 & 0xFF);
@@ -150,13 +157,15 @@ public class IpUtils
150
                     for (i = 0; i < 2; ++i)
157
                     for (i = 0; i < 2; ++i)
151
                     {
158
                     {
152
                         l = Integer.parseInt(elements[i]);
159
                         l = Integer.parseInt(elements[i]);
153
-                        if ((l < 0L) || (l > 255L)) {
160
+                        if ((l < 0L) || (l > 255L))
161
+                        {
154
                             return null;
162
                             return null;
155
                         }
163
                         }
156
                         bytes[i] = (byte) (int) (l & 0xFF);
164
                         bytes[i] = (byte) (int) (l & 0xFF);
157
                     }
165
                     }
158
                     l = Integer.parseInt(elements[2]);
166
                     l = Integer.parseInt(elements[2]);
159
-                    if ((l < 0L) || (l > 65535L)) {
167
+                    if ((l < 0L) || (l > 65535L))
168
+                    {
160
                         return null;
169
                         return null;
161
                     }
170
                     }
162
                     bytes[2] = (byte) (int) (l >> 8 & 0xFF);
171
                     bytes[2] = (byte) (int) (l >> 8 & 0xFF);
@@ -166,7 +175,8 @@ public class IpUtils
166
                     for (i = 0; i < 4; ++i)
175
                     for (i = 0; i < 4; ++i)
167
                     {
176
                     {
168
                         l = Integer.parseInt(elements[i]);
177
                         l = Integer.parseInt(elements[i]);
169
-                        if ((l < 0L) || (l > 255L)) {
178
+                        if ((l < 0L) || (l > 255L))
179
+                        {
170
                             return null;
180
                             return null;
171
                         }
181
                         }
172
                         bytes[i] = (byte) (int) (l & 0xFF);
182
                         bytes[i] = (byte) (int) (l & 0xFF);
@@ -183,6 +193,11 @@ public class IpUtils
183
         return bytes;
193
         return bytes;
184
     }
194
     }
185
 
195
 
196
+    /**
197
+     * 获取IP地址
198
+     * 
199
+     * @return 本地IP地址
200
+     */
186
     public static String getHostIp()
201
     public static String getHostIp()
187
     {
202
     {
188
         try
203
         try
@@ -195,6 +210,11 @@ public class IpUtils
195
         return "127.0.0.1";
210
         return "127.0.0.1";
196
     }
211
     }
197
 
212
 
213
+    /**
214
+     * 获取主机名
215
+     * 
216
+     * @return 本地主机名
217
+     */
198
     public static String getHostName()
218
     public static String getHostName()
199
     {
219
     {
200
         try
220
         try
@@ -206,4 +226,39 @@ public class IpUtils
206
         }
226
         }
207
         return "未知";
227
         return "未知";
208
     }
228
     }
229
+
230
+    /**
231
+     * 从多级反向代理中获得第一个非unknown IP地址
232
+     *
233
+     * @param ip 获得的IP地址
234
+     * @return 第一个非unknown IP地址
235
+     */
236
+    public static String getMultistageReverseProxyIp(String ip)
237
+    {
238
+        // 多级反向代理检测
239
+        if (ip != null && ip.indexOf(",") > 0)
240
+        {
241
+            final String[] ips = ip.trim().split(",");
242
+            for (String subIp : ips)
243
+            {
244
+                if (false == isUnknown(subIp))
245
+                {
246
+                    ip = subIp;
247
+                    break;
248
+                }
249
+            }
250
+        }
251
+        return ip;
252
+    }
253
+
254
+    /**
255
+     * 检测给定字符串是否为未知,多用于检测HTTP请求相关
256
+     *
257
+     * @param checkString 被检测的字符串
258
+     * @return 是否未知
259
+     */
260
+    public static boolean isUnknown(String checkString)
261
+    {
262
+        return StringUtils.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);
263
+    }
209
 }
264
 }