IpUtils.java 7.5 KB

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