SmartIPUtil.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package net.lab1024.smartadmin.util;
  2. import java.net.InetAddress;
  3. import java.net.NetworkInterface;
  4. import java.net.SocketException;
  5. import java.util.Enumeration;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import javax.servlet.http.HttpServletRequest;
  9. import org.apache.commons.lang3.StringUtils;
  10. import com.alibaba.fastjson.JSON;
  11. import com.alibaba.fastjson.JSONObject;
  12. /**
  13. * [ ]
  14. *
  15. * @author yandanyang
  16. * @version 1.0
  17. * @company 1024lab.net
  18. * @copyright (c) 2018 1024lab.netInc. All rights reserved.
  19. * @date 2019/5/5 0005 下午 15:34
  20. * @since JDK1.8
  21. */
  22. public class SmartIPUtil {
  23. public static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php";
  24. public static String getLocalHostIP() {
  25. // 本地IP,如果没有配置外网IP则返回它
  26. String localIp = null;
  27. // 外网IP
  28. String netIp = null;
  29. try {
  30. Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
  31. InetAddress ip = null;
  32. // 是否找到外网IP
  33. boolean finded = false;
  34. while (netInterfaces.hasMoreElements() && ! finded) {
  35. NetworkInterface ni = netInterfaces.nextElement();
  36. Enumeration<InetAddress> address = ni.getInetAddresses();
  37. while (address.hasMoreElements()) {
  38. ip = address.nextElement();
  39. // 外网IP
  40. if (! ip.isSiteLocalAddress() && ! ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == - 1) {
  41. netIp = ip.getHostAddress();
  42. finded = true;
  43. break;
  44. } else if (ip.isSiteLocalAddress() && ! ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == - 1) {
  45. // 内网IP
  46. localIp = ip.getHostAddress();
  47. }
  48. }
  49. }
  50. } catch (SocketException e) {
  51. e.getMessage();
  52. }
  53. if (netIp != null && ! "".equals(netIp)) {
  54. return netIp;
  55. } else {
  56. return localIp;
  57. }
  58. }
  59. public static String getRemoteIp(HttpServletRequest request) {
  60. // 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址
  61. String ip = getXForwardedForIp(request);
  62. if (ipValid(ip)) {
  63. return realIp(ip);
  64. }
  65. ip = request.getHeader("Proxy-Client-IP");
  66. if (ipValid(ip)) {
  67. return realIp(ip);
  68. }
  69. ip = request.getHeader("HTTP_CLIENT_IP");
  70. if (ipValid(ip)) {
  71. return realIp(ip);
  72. }
  73. ip = request.getHeader("HTTP_X_FORWARDED_FOR");
  74. if (ipValid(ip)) {
  75. return realIp(ip);
  76. }
  77. ip = request.getRemoteAddr();
  78. return realIp(ip);
  79. }
  80. private static String getXForwardedForIp(HttpServletRequest request) {
  81. String ip = request.getHeader("x-forwarded-for");
  82. //ip 无效直接返回
  83. if (! ipValid(ip)) {
  84. return "";
  85. }
  86. if (ip.length() > 15) {
  87. String[] ips = ip.split(",");
  88. for (String strIp : ips) {
  89. if (! ("unknown".equalsIgnoreCase(strIp))) {
  90. ip = strIp;
  91. break;
  92. }
  93. }
  94. return ip;
  95. }
  96. return ip;
  97. }
  98. private static Boolean ipValid(String ip) {
  99. if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
  100. return false;
  101. }
  102. return true;
  103. }
  104. private static String realIp(String ip) {
  105. if (StringUtils.isEmpty(ip)) {
  106. return "";
  107. }
  108. return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
  109. }
  110. public static String getRemoteLocation(HttpServletRequest request) {
  111. String ip = getRemoteIp(request);
  112. return getIpLocation(ip);
  113. }
  114. public static String getIpLocation(String ip) {
  115. String location = "未知";
  116. if (StringUtils.isEmpty(ip)) {
  117. return location;
  118. }
  119. Map<String, Object> param = new HashMap<>();
  120. param.put("ip", ip);
  121. try {
  122. String rspStr = SmartHttpUtil.sendGet(IP_URL, param, null);
  123. if (StringUtils.isEmpty(rspStr)) {
  124. return location;
  125. }
  126. JSONObject jsonObject = JSON.parseObject(rspStr);
  127. String data = jsonObject.getString("data");
  128. JSONObject jsonData = JSON.parseObject(data);
  129. String region = jsonData.getString("region");
  130. String city = jsonData.getString("city");
  131. location = region + " " + city;
  132. if (location.contains("内网IP")) {
  133. location = "内网(" + ip + ")";
  134. }
  135. } catch (Exception e) {
  136. }
  137. return location;
  138. }
  139. public static void main(String[] args) {
  140. System.out.printf(getIpLocation("172.16.0.221"));
  141. }
  142. }