简体   繁体   中英

Getting ipv4 instead of ipv6 in jsf

I want to get the IP address of a client in a remote machine.I am using this code :

public static String getClientIpAddr(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();
    }
    return ip;
}

But it always returns the IPv6 , is there a way to have an IPv4 ?

If you get an IPv6 address then IPv6 is being used. IPv4 and IPv6 are different protocols, and the client chooses which one to use when both are available.

If you want the client's IPv4 address then you can force them to use it by not advertising the IPv6 address in DNS. That would be a bad idea though with the increasing deployment of IPv6. Supporting both is good, so it is better to deal with clients using IPv6.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM