作者 RuoYi

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

@@ -4,7 +4,6 @@ import java.net.InetAddress; @@ -4,7 +4,6 @@ import java.net.InetAddress;
4 import java.net.UnknownHostException; 4 import java.net.UnknownHostException;
5 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletRequest;
6 import com.ruoyi.common.utils.StringUtils; 6 import com.ruoyi.common.utils.StringUtils;
7 -import com.ruoyi.common.utils.html.EscapeUtil;  
8 7
9 /** 8 /**
10 * 获取IP方法 9 * 获取IP方法
@@ -13,6 +12,12 @@ import com.ruoyi.common.utils.html.EscapeUtil; @@ -13,6 +12,12 @@ import com.ruoyi.common.utils.html.EscapeUtil;
13 */ 12 */
14 public class IpUtils 13 public class IpUtils
15 { 14 {
  15 + /**
  16 + * 获取客户端IP
  17 + *
  18 + * @param request 请求对象
  19 + * @return IP地址
  20 + */
16 public static String getIpAddr(HttpServletRequest request) 21 public static String getIpAddr(HttpServletRequest request)
17 { 22 {
18 if (request == null) 23 if (request == null)
@@ -41,15 +46,28 @@ public class IpUtils @@ -41,15 +46,28 @@ public class IpUtils
41 { 46 {
42 ip = request.getRemoteAddr(); 47 ip = request.getRemoteAddr();
43 } 48 }
44 - return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : EscapeUtil.clean(ip); 49 +
  50 + return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : getMultistageReverseProxyIp(ip);
45 } 51 }
46 52
  53 + /**
  54 + * 检查是否为内部IP地址
  55 + *
  56 + * @param ip IP地址
  57 + * @return 结果
  58 + */
47 public static boolean internalIp(String ip) 59 public static boolean internalIp(String ip)
48 { 60 {
49 byte[] addr = textToNumericFormatV4(ip); 61 byte[] addr = textToNumericFormatV4(ip);
50 return internalIp(addr) || "127.0.0.1".equals(ip); 62 return internalIp(addr) || "127.0.0.1".equals(ip);
51 } 63 }
52 64
  65 + /**
  66 + * 检查是否为内部IP地址
  67 + *
  68 + * @param addr byte地址
  69 + * @return 结果
  70 + */
53 private static boolean internalIp(byte[] addr) 71 private static boolean internalIp(byte[] addr)
54 { 72 {
55 if (StringUtils.isNull(addr) || addr.length < 2) 73 if (StringUtils.isNull(addr) || addr.length < 2)
@@ -110,7 +128,8 @@ public class IpUtils @@ -110,7 +128,8 @@ public class IpUtils
110 { 128 {
111 case 1: 129 case 1:
112 l = Long.parseLong(elements[0]); 130 l = Long.parseLong(elements[0]);
113 - if ((l < 0L) || (l > 4294967295L)) { 131 + if ((l < 0L) || (l > 4294967295L))
  132 + {
114 return null; 133 return null;
115 } 134 }
116 bytes[0] = (byte) (int) (l >> 24 & 0xFF); 135 bytes[0] = (byte) (int) (l >> 24 & 0xFF);
@@ -120,12 +139,14 @@ public class IpUtils @@ -120,12 +139,14 @@ public class IpUtils
120 break; 139 break;
121 case 2: 140 case 2:
122 l = Integer.parseInt(elements[0]); 141 l = Integer.parseInt(elements[0]);
123 - if ((l < 0L) || (l > 255L)) { 142 + if ((l < 0L) || (l > 255L))
  143 + {
124 return null; 144 return null;
125 } 145 }
126 bytes[0] = (byte) (int) (l & 0xFF); 146 bytes[0] = (byte) (int) (l & 0xFF);
127 l = Integer.parseInt(elements[1]); 147 l = Integer.parseInt(elements[1]);
128 - if ((l < 0L) || (l > 16777215L)) { 148 + if ((l < 0L) || (l > 16777215L))
  149 + {
129 return null; 150 return null;
130 } 151 }
131 bytes[1] = (byte) (int) (l >> 16 & 0xFF); 152 bytes[1] = (byte) (int) (l >> 16 & 0xFF);
@@ -136,13 +157,15 @@ public class IpUtils @@ -136,13 +157,15 @@ public class IpUtils
136 for (i = 0; i < 2; ++i) 157 for (i = 0; i < 2; ++i)
137 { 158 {
138 l = Integer.parseInt(elements[i]); 159 l = Integer.parseInt(elements[i]);
139 - if ((l < 0L) || (l > 255L)) { 160 + if ((l < 0L) || (l > 255L))
  161 + {
140 return null; 162 return null;
141 } 163 }
142 bytes[i] = (byte) (int) (l & 0xFF); 164 bytes[i] = (byte) (int) (l & 0xFF);
143 } 165 }
144 l = Integer.parseInt(elements[2]); 166 l = Integer.parseInt(elements[2]);
145 - if ((l < 0L) || (l > 65535L)) { 167 + if ((l < 0L) || (l > 65535L))
  168 + {
146 return null; 169 return null;
147 } 170 }
148 bytes[2] = (byte) (int) (l >> 8 & 0xFF); 171 bytes[2] = (byte) (int) (l >> 8 & 0xFF);
@@ -152,7 +175,8 @@ public class IpUtils @@ -152,7 +175,8 @@ public class IpUtils
152 for (i = 0; i < 4; ++i) 175 for (i = 0; i < 4; ++i)
153 { 176 {
154 l = Integer.parseInt(elements[i]); 177 l = Integer.parseInt(elements[i]);
155 - if ((l < 0L) || (l > 255L)) { 178 + if ((l < 0L) || (l > 255L))
  179 + {
156 return null; 180 return null;
157 } 181 }
158 bytes[i] = (byte) (int) (l & 0xFF); 182 bytes[i] = (byte) (int) (l & 0xFF);
@@ -169,6 +193,11 @@ public class IpUtils @@ -169,6 +193,11 @@ public class IpUtils
169 return bytes; 193 return bytes;
170 } 194 }
171 195
  196 + /**
  197 + * 获取IP地址
  198 + *
  199 + * @return 本地IP地址
  200 + */
172 public static String getHostIp() 201 public static String getHostIp()
173 { 202 {
174 try 203 try
@@ -181,6 +210,11 @@ public class IpUtils @@ -181,6 +210,11 @@ public class IpUtils
181 return "127.0.0.1"; 210 return "127.0.0.1";
182 } 211 }
183 212
  213 + /**
  214 + * 获取主机名
  215 + *
  216 + * @return 本地主机名
  217 + */
184 public static String getHostName() 218 public static String getHostName()
185 { 219 {
186 try 220 try
@@ -192,4 +226,39 @@ public class IpUtils @@ -192,4 +226,39 @@ public class IpUtils
192 } 226 }
193 return "未知"; 227 return "未知";
194 } 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 + }
195 } 264 }