IpUtils.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package com.ruoyi.common.utils;
  2. import java.net.InetAddress;
  3. import java.net.UnknownHostException;
  4. import javax.servlet.http.HttpServletRequest;
  5. /**
  6. * 获取IP方法
  7. *
  8. * @author ruoyi
  9. */
  10. public class IpUtils
  11. {
  12. /**
  13. * 获取客户端IP
  14. *
  15. * @param request 请求对象
  16. * @return IP地址
  17. */
  18. public static String getIpAddr(HttpServletRequest request)
  19. {
  20. if (request == null)
  21. {
  22. return "unknown";
  23. }
  24. String ip = request.getHeader("x-forwarded-for");
  25. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  26. {
  27. ip = request.getHeader("Proxy-Client-IP");
  28. }
  29. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  30. {
  31. ip = request.getHeader("X-Forwarded-For");
  32. }
  33. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  34. {
  35. ip = request.getHeader("WL-Proxy-Client-IP");
  36. }
  37. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  38. {
  39. ip = request.getHeader("X-Real-IP");
  40. }
  41. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  42. {
  43. ip = request.getRemoteAddr();
  44. }
  45. return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : getMultistageReverseProxyIp(ip);
  46. }
  47. /**
  48. * 检查是否为内部IP地址
  49. *
  50. * @param ip IP地址
  51. * @return 结果
  52. */
  53. public static boolean internalIp(String ip)
  54. {
  55. byte[] addr = textToNumericFormatV4(ip);
  56. return internalIp(addr) || "127.0.0.1".equals(ip);
  57. }
  58. /**
  59. * 检查是否为内部IP地址
  60. *
  61. * @param addr byte地址
  62. * @return 结果
  63. */
  64. private static boolean internalIp(byte[] addr)
  65. {
  66. if (StringUtils.isNull(addr) || addr.length < 2)
  67. {
  68. return true;
  69. }
  70. final byte b0 = addr[0];
  71. final byte b1 = addr[1];
  72. // 10.x.x.x/8
  73. final byte SECTION_1 = 0x0A;
  74. // 172.16.x.x/12
  75. final byte SECTION_2 = (byte) 0xAC;
  76. final byte SECTION_3 = (byte) 0x10;
  77. final byte SECTION_4 = (byte) 0x1F;
  78. // 192.168.x.x/16
  79. final byte SECTION_5 = (byte) 0xC0;
  80. final byte SECTION_6 = (byte) 0xA8;
  81. switch (b0)
  82. {
  83. case SECTION_1:
  84. return true;
  85. case SECTION_2:
  86. if (b1 >= SECTION_3 && b1 <= SECTION_4)
  87. {
  88. return true;
  89. }
  90. case SECTION_5:
  91. switch (b1)
  92. {
  93. case SECTION_6:
  94. return true;
  95. }
  96. default:
  97. return false;
  98. }
  99. }
  100. /**
  101. * 将IPv4地址转换成字节
  102. *
  103. * @param text IPv4地址
  104. * @return byte 字节
  105. */
  106. public static byte[] textToNumericFormatV4(String text)
  107. {
  108. if (text.length() == 0)
  109. {
  110. return null;
  111. }
  112. byte[] bytes = new byte[4];
  113. String[] elements = text.split("\\.", -1);
  114. try
  115. {
  116. long l;
  117. int i;
  118. switch (elements.length)
  119. {
  120. case 1:
  121. l = Long.parseLong(elements[0]);
  122. if ((l < 0L) || (l > 4294967295L))
  123. {
  124. return null;
  125. }
  126. bytes[0] = (byte) (int) (l >> 24 & 0xFF);
  127. bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
  128. bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
  129. bytes[3] = (byte) (int) (l & 0xFF);
  130. break;
  131. case 2:
  132. l = Integer.parseInt(elements[0]);
  133. if ((l < 0L) || (l > 255L))
  134. {
  135. return null;
  136. }
  137. bytes[0] = (byte) (int) (l & 0xFF);
  138. l = Integer.parseInt(elements[1]);
  139. if ((l < 0L) || (l > 16777215L))
  140. {
  141. return null;
  142. }
  143. bytes[1] = (byte) (int) (l >> 16 & 0xFF);
  144. bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
  145. bytes[3] = (byte) (int) (l & 0xFF);
  146. break;
  147. case 3:
  148. for (i = 0; i < 2; ++i)
  149. {
  150. l = Integer.parseInt(elements[i]);
  151. if ((l < 0L) || (l > 255L))
  152. {
  153. return null;
  154. }
  155. bytes[i] = (byte) (int) (l & 0xFF);
  156. }
  157. l = Integer.parseInt(elements[2]);
  158. if ((l < 0L) || (l > 65535L))
  159. {
  160. return null;
  161. }
  162. bytes[2] = (byte) (int) (l >> 8 & 0xFF);
  163. bytes[3] = (byte) (int) (l & 0xFF);
  164. break;
  165. case 4:
  166. for (i = 0; i < 4; ++i)
  167. {
  168. l = Integer.parseInt(elements[i]);
  169. if ((l < 0L) || (l > 255L))
  170. {
  171. return null;
  172. }
  173. bytes[i] = (byte) (int) (l & 0xFF);
  174. }
  175. break;
  176. default:
  177. return null;
  178. }
  179. }
  180. catch (NumberFormatException e)
  181. {
  182. return null;
  183. }
  184. return bytes;
  185. }
  186. /**
  187. * 获取IP地址
  188. *
  189. * @return 本地IP地址
  190. */
  191. public static String getHostIp()
  192. {
  193. try
  194. {
  195. return InetAddress.getLocalHost().getHostAddress();
  196. }
  197. catch (UnknownHostException e)
  198. {
  199. }
  200. return "127.0.0.1";
  201. }
  202. /**
  203. * 获取主机名
  204. *
  205. * @return 本地主机名
  206. */
  207. public static String getHostName()
  208. {
  209. try
  210. {
  211. return InetAddress.getLocalHost().getHostName();
  212. }
  213. catch (UnknownHostException e)
  214. {
  215. }
  216. return "未知";
  217. }
  218. /**
  219. * 从多级反向代理中获得第一个非unknown IP地址
  220. *
  221. * @param ip 获得的IP地址
  222. * @return 第一个非unknown IP地址
  223. */
  224. public static String getMultistageReverseProxyIp(String ip)
  225. {
  226. // 多级反向代理检测
  227. if (ip != null && ip.indexOf(",") > 0)
  228. {
  229. final String[] ips = ip.trim().split(",");
  230. for (String subIp : ips)
  231. {
  232. if (false == isUnknown(subIp))
  233. {
  234. ip = subIp;
  235. break;
  236. }
  237. }
  238. }
  239. return ip;
  240. }
  241. /**
  242. * 检测给定字符串是否为未知,多用于检测HTTP请求相关
  243. *
  244. * @param checkString 被检测的字符串
  245. * @return 是否未知
  246. */
  247. public static boolean isUnknown(String checkString)
  248. {
  249. return StringUtils.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);
  250. }
  251. }