IpUtils.java 5.0 KB

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