IpUtils.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. public static String getIpAddr(HttpServletRequest request)
  14. {
  15. if (request == null)
  16. {
  17. return "unknown";
  18. }
  19. String ip = request.getHeader("x-forwarded-for");
  20. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  21. {
  22. ip = request.getHeader("Proxy-Client-IP");
  23. }
  24. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  25. {
  26. ip = request.getHeader("X-Forwarded-For");
  27. }
  28. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  29. {
  30. ip = request.getHeader("WL-Proxy-Client-IP");
  31. }
  32. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  33. {
  34. ip = request.getHeader("X-Real-IP");
  35. }
  36. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  37. {
  38. ip = request.getRemoteAddr();
  39. }
  40. ip = EscapeUtil.clean(ip);//清除Xss特殊字符
  41. return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : 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 (StringUtils.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. bytes[0] = (byte) (int) (l >> 24 & 0xFF);
  109. bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
  110. bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
  111. bytes[3] = (byte) (int) (l & 0xFF);
  112. break;
  113. case 2:
  114. l = Integer.parseInt(elements[0]);
  115. if ((l < 0L) || (l > 255L))
  116. return null;
  117. bytes[0] = (byte) (int) (l & 0xFF);
  118. l = Integer.parseInt(elements[1]);
  119. if ((l < 0L) || (l > 16777215L))
  120. return null;
  121. bytes[1] = (byte) (int) (l >> 16 & 0xFF);
  122. bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
  123. bytes[3] = (byte) (int) (l & 0xFF);
  124. break;
  125. case 3:
  126. for (i = 0; i < 2; ++i)
  127. {
  128. l = Integer.parseInt(elements[i]);
  129. if ((l < 0L) || (l > 255L))
  130. return null;
  131. bytes[i] = (byte) (int) (l & 0xFF);
  132. }
  133. l = Integer.parseInt(elements[2]);
  134. if ((l < 0L) || (l > 65535L))
  135. return null;
  136. bytes[2] = (byte) (int) (l >> 8 & 0xFF);
  137. bytes[3] = (byte) (int) (l & 0xFF);
  138. break;
  139. case 4:
  140. for (i = 0; i < 4; ++i)
  141. {
  142. l = Integer.parseInt(elements[i]);
  143. if ((l < 0L) || (l > 255L))
  144. return null;
  145. bytes[i] = (byte) (int) (l & 0xFF);
  146. }
  147. break;
  148. default:
  149. return null;
  150. }
  151. }
  152. catch (NumberFormatException e)
  153. {
  154. return null;
  155. }
  156. return bytes;
  157. }
  158. public static String getHostIp()
  159. {
  160. try
  161. {
  162. return InetAddress.getLocalHost().getHostAddress();
  163. }
  164. catch (UnknownHostException e)
  165. {
  166. }
  167. return "127.0.0.1";
  168. }
  169. public static String getHostName()
  170. {
  171. try
  172. {
  173. return InetAddress.getLocalHost().getHostName();
  174. }
  175. catch (UnknownHostException e)
  176. {
  177. }
  178. return "未知";
  179. }
  180. }