Java获取客户端、本机IP

获取客户端IP

某些情况下需要或取用户客户端的ip,用来统计或者拦截,比如有些网站是需要屏蔽海外ip的访问的,这就需要获取客户端的访问ip。一般情况下用HttpServletRequest对象的getRemoteAddr()方法即可获取。但是当请求经过HTTP代理或反向代理(如ngrok、nginx等)后这样获取ip是不正确的,这有点类似于跳板机。服务经过反向代理后拿到的IP已经不是"第一层"IP了,而是代理服务器这个"中介"的IP了,有时候甚至存在多级反向代理,那这个时候如何准确的获取客户端的IP呢?原来在存在代理的情况下,请求头会加上一些特定信息,如:x-forwarded-for、Proxy-Client-IP、WL-Proxy-Client-IP等来指向ip地址,于是乎获取ip变得简单起来。
public static String getIpAddr(HttpServletRequest request) {
   String ip = request.getHeader("x-forwarded-for");
   if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("Proxy-Client-IP");
   }
   if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("WL-Proxy-Client-IP");
   }
   if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getRemoteAddr();
   }
   return ip;
}
存在多级代理时,则取第一个非 unknown 的IP
   public static String getIpAddrAdvanced(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        if (ip != null && ip.indexOf(",") != -1) {
            String[] ipWithMultiProxy = ip.split(",");

            for(int i = 0; i < ipWithMultiProxy.length; ++i) {
                String eachIpSegement = ipWithMultiProxy[i];
                if (!"unknown".equalsIgnoreCase(eachIpSegement)) {
                    ip = eachIpSegement;
                    break;
                }
            }
        }

        return ip;
    }

获取本机IP

这个有什么应用场景呢?举个栗子,分布式应用环境中,为了保证定时任务只在单台服务器上跑,可在定时任务的逻辑中加上ip的判断(服务器,比如说公网linux的ip是已知的),来决定哪些服务器上可以跑定时任务(当然,这种场景也可以使用zookeeper客户端Curator实现或者分布式锁实现)

    public String getLocalIP() throws Exception {
        try {
            InetAddress candidateAddress = null;
            for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) {
                NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
                for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) {
                    InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
                    if (!inetAddr.isLoopbackAddress()) {
                        if (inetAddr.isSiteLocalAddress()) {
                            return inetAddr.getHostAddress();
                        } else if (candidateAddress == null) {
                            candidateAddress = inetAddr;
                        }
                    }
                }
            }
            if (candidateAddress != null) {
                return candidateAddress.getHostAddress();
            }
            InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
            return jdkSuppliedAddress.getHostAddress();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页