IpUtils.java 5.6 KB

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