IpUtils.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package com.ruoyi.common.utils.ip;
  2. import cn.hutool.core.lang.Validator;
  3. import cn.hutool.http.HtmlUtil;
  4. import javax.servlet.http.HttpServletRequest;
  5. import java.net.InetAddress;
  6. import java.net.UnknownHostException;
  7. /**
  8. * 获取IP方法
  9. *
  10. * @author ruoyi
  11. */
  12. public class IpUtils
  13. {
  14. public static String getIpAddr(HttpServletRequest request)
  15. {
  16. if (request == null)
  17. {
  18. return "unknown";
  19. }
  20. String ip = request.getHeader("x-forwarded-for");
  21. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  22. {
  23. ip = request.getHeader("Proxy-Client-IP");
  24. }
  25. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  26. {
  27. ip = request.getHeader("X-Forwarded-For");
  28. }
  29. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  30. {
  31. ip = request.getHeader("WL-Proxy-Client-IP");
  32. }
  33. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  34. {
  35. ip = request.getHeader("X-Real-IP");
  36. }
  37. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  38. {
  39. ip = request.getRemoteAddr();
  40. }
  41. return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : HtmlUtil.cleanHtmlTag(ip);
  42. }
  43. public static boolean internalIp(String ip)
  44. {
  45. byte[] addr = textToNumericFormatV4(ip);
  46. return internalIp(addr) || "127.0.0.1".equals(ip);
  47. }
  48. private static boolean internalIp(byte[] addr)
  49. {
  50. if (Validator.isNull(addr) || addr.length < 2)
  51. {
  52. return true;
  53. }
  54. final byte b0 = addr[0];
  55. final byte b1 = addr[1];
  56. // 10.x.x.x/8
  57. final byte SECTION_1 = 0x0A;
  58. // 172.16.x.x/12
  59. final byte SECTION_2 = (byte) 0xAC;
  60. final byte SECTION_3 = (byte) 0x10;
  61. final byte SECTION_4 = (byte) 0x1F;
  62. // 192.168.x.x/16
  63. final byte SECTION_5 = (byte) 0xC0;
  64. final byte SECTION_6 = (byte) 0xA8;
  65. switch (b0)
  66. {
  67. case SECTION_1:
  68. return true;
  69. case SECTION_2:
  70. if (b1 >= SECTION_3 && b1 <= SECTION_4)
  71. {
  72. return true;
  73. }
  74. case SECTION_5:
  75. switch (b1)
  76. {
  77. case SECTION_6:
  78. return true;
  79. }
  80. default:
  81. return false;
  82. }
  83. }
  84. /**
  85. * 将IPv4地址转换成字节
  86. *
  87. * @param text IPv4地址
  88. * @return byte 字节
  89. */
  90. public static byte[] textToNumericFormatV4(String text)
  91. {
  92. if (text.length() == 0)
  93. {
  94. return null;
  95. }
  96. byte[] bytes = new byte[4];
  97. String[] elements = text.split("\\.", -1);
  98. try
  99. {
  100. long l;
  101. int i;
  102. switch (elements.length)
  103. {
  104. case 1:
  105. l = Long.parseLong(elements[0]);
  106. if ((l < 0L) || (l > 4294967295L)) {
  107. return null;
  108. }
  109. bytes[0] = (byte) (int) (l >> 24 & 0xFF);
  110. bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
  111. bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
  112. bytes[3] = (byte) (int) (l & 0xFF);
  113. break;
  114. case 2:
  115. l = Integer.parseInt(elements[0]);
  116. if ((l < 0L) || (l > 255L)) {
  117. return null;
  118. }
  119. bytes[0] = (byte) (int) (l & 0xFF);
  120. l = Integer.parseInt(elements[1]);
  121. if ((l < 0L) || (l > 16777215L)) {
  122. return null;
  123. }
  124. bytes[1] = (byte) (int) (l >> 16 & 0xFF);
  125. bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
  126. bytes[3] = (byte) (int) (l & 0xFF);
  127. break;
  128. case 3:
  129. for (i = 0; i < 2; ++i)
  130. {
  131. l = Integer.parseInt(elements[i]);
  132. if ((l < 0L) || (l > 255L)) {
  133. return null;
  134. }
  135. bytes[i] = (byte) (int) (l & 0xFF);
  136. }
  137. l = Integer.parseInt(elements[2]);
  138. if ((l < 0L) || (l > 65535L)) {
  139. return null;
  140. }
  141. bytes[2] = (byte) (int) (l >> 8 & 0xFF);
  142. bytes[3] = (byte) (int) (l & 0xFF);
  143. break;
  144. case 4:
  145. for (i = 0; i < 4; ++i)
  146. {
  147. l = Integer.parseInt(elements[i]);
  148. if ((l < 0L) || (l > 255L)) {
  149. return null;
  150. }
  151. bytes[i] = (byte) (int) (l & 0xFF);
  152. }
  153. break;
  154. default:
  155. return null;
  156. }
  157. }
  158. catch (NumberFormatException e)
  159. {
  160. return null;
  161. }
  162. return bytes;
  163. }
  164. public static String getHostIp()
  165. {
  166. try
  167. {
  168. return InetAddress.getLocalHost().getHostAddress();
  169. }
  170. catch (UnknownHostException e)
  171. {
  172. }
  173. return "127.0.0.1";
  174. }
  175. public static String getHostName()
  176. {
  177. try
  178. {
  179. return InetAddress.getLocalHost().getHostName();
  180. }
  181. catch (UnknownHostException e)
  182. {
  183. }
  184. return "未知";
  185. }
  186. }