Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top