第三方兼职平台靠谱吗,抖音等APP显示了用户的地区,技术层面是如何做到的?

时间:2022-05-07 阅读:11 评论:0 作者:daiit

  要获取的是用户客户端的IP地址,还是服务器IP地址。   要获取的是局域网的IP地址还是公网IP地址。   如果用代理的话该怎么获取真实的IP地址。   import javax.servlet.http.HttpServletRequest;   import java.net.InetAddress;   import java.net.UnknownHostException;   public class IPUtils {   /**   * 获取用户端的ip地址   */   public static String getClientIpAddr(HttpServletRequest request) {   String ipAddress = request.getHeader("X-Real-IP");   if (invalidIp(ipAddress)) {   String forwardIp = request.getHeader("x-forwarded-for");   if (!invalidIp(forwardIp)) {   ipAddress = forwardIp;   }   }   if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {   ipAddress = request.getHeader("Proxy-Client-IP");   }   if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {   ipAddress = request.getHeader("WL-抖音Proxy-Client-IP");   }   if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {   ipAddress = request.getRemoteAddr();   if ("127.0.0.1".equals(ipAddress) || "0:0:0:0:0:0:0:1".equals(ipAddress)) {   // 根据网卡取本机配置的IP   InetAddress inet = null;   try {   inet = InetAddress.getLocalHost();   } catch (UnknownHostException e) {   e.printStackTrace();   }   ipAddress = inet.getHostAddress();   }   }   //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割   if (ipAddress != null && ipAddress.length() > 15) { //"***.***.***.***".length() = 15   if (ipAddress.indexOf(",") > 0) {   ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));   }   }   log.info("ipTestInfo: backendReal={},realIp={},forwardIp={},proxyClientIp={},wlProxyClientIp={},remoteAddr={},ip={}", request.getHeader("X-BACKEND-BILI-REAL-IP"), request.getHeader("X-Real-IP"),   request.getHeader("x-forwarded-for"), request.getHeader("Proxy-Client-IP"), request.getHeader("WL-Proxy-Client-IP"), request.getRemoteAddr(), ipAddress);   return ipAddress;   }   private static boolean invalidIp(String ipAddress) {   return ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress) || isInnerIP(ipAddress);   }   /**   * 判断是否内网ip   */   public static boolean isInnerIP(String ipAddress) {   boolean isInnerIp = false;   try {   long ipNum = getIpNum(ipAddress);   /**   私有IP:A类 10.0.0.0-10.255.255.255   B类 172.16.0.0-172.31.255.255   C类 192.168.0.0-192.168.255.255   当然,还有127这个网段是环回地址   **/   long aBegin = getIpNum("10.0.0.0");   long aEnd = getIpNum("10.255.255.255");   long bBegin = getIpNum("172.16.0.0");   long bEnd = getIpNum("172.31.255.255");   long cBegin = getIpNum("192.168.0.0");   long cEnd = getIpNum("192.168.255.255");   isInnerIp = isInner(ipNum, aBegin, aEnd) || isInner(ipNum, bBegin, bEnd) || isInner(ipNum, cBegin, cEnd) || ipAddress.equals("127.0.0.1");   } catch (Exception e) {   log.error("get innerIP error,", e);   }   return isInnerIp;   }   private static long getIpNum(String ipAddress) {   String[] ip = ipAddress.split("\.");   long a = Integer.parseInt(ip[0]);   long b = Integer.parseInt(ip[1]);   long c = Integer.parseInt(ip[2]);   long d = Integer.parseInt(ip[3]);   long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;   return ipNum;   }   private static boolean isInner(long userIp, long begin, long end) {   return (userIp >= begin) && (userIp <= end);   }   }

本文链接: http://52kmv.cn/a/122.html 转载请注明出处!

评论区 交流一下吧!

共有0条评论来说两句吧...

欢迎 发表评论: