简体   繁体   English

如何找出“正确的” IP地址?

[英]How to find out “the right” IP address?

I have a problem with InetAddress.getLocalHost().getHostAddress(). 我有InetAddress.getLocalHost()。getHostAddress()问题。 It works correctly on most machines, but it fails on one where there are more IP addresses available (in this case "the wrong" address belongs to VMware network adapter). 它可以在大多数计算机上正常工作,但是在有更多IP地址可用的计算机上失败(在这种情况下,“错误的”地址属于VMware网络适配器)。 I need the address to put it into a message (which then is used on the server as an address where a response should be sent). 我需要将该地址放入邮件中(然后在服务器上用作发送响应的地址)。

I know that I may use NetworkInterface.getNetworkInterfaces() to get all network interfaces but how I may programatically find the right one which is later visible for the server? 我知道我可以使用NetworkInterface.getNetworkInterfaces()来获取所有网络接口,但是如何以编程方式找到正确的接口,以后该服务器才可以看到? In my particular case both clients and the server are located inside the same corporate network. 在我的特殊情况下,客户端和服务器都位于同一公司网络内。

If all machines are in the same network and this network has its IP range, you may check if IP is in this range. 如果所有计算机都在同一网络中,并且该网络具有其IP范围,则可以检查IP是否在此范围内。 Usually vmware network adapters have IPs in 192.168.0.x subnet - if your corporate range is different, then it should be enough. 通常,vmware网络适配器的IP地址在192.168.0.x子网中-如果您的公司范围不同,那么就足够了。

Maybe the following class to help with IP 也许以下课程可以帮助您解决IP

public enum IpAddressHelper
{

    X_FORWARDED_FOR("X-Forwarded-For"),
    PROXY_CLIENT_IP("Proxy-Client-IP"),
    WL_PROXY_CLIENT_IP("WL-Proxy-Client-IP"),
    HTTP_CLIENT_IP("HTTP_CLIENT_IP"),
    HTTP_X_FORWARDED_FOR("HTTP_X_FORWARDED_FOR");

    private static final Logger LOGGER = LoggerFactory.getLogger(IpAddressHelper.class);
    private static final String REMOTE_ADDR = "REMOTE_ADDR";
    private String key;

    /**
     * @param key
     */
    IpAddressHelper(String key)
    {
        this.key = key;
    }

    /**
     * @return the key
     */
    public String getKey()
    {
        return key;
    }

    public static String getClientIpAddr(HttpServletRequest request)
    {

        String ip = null;
        for (IpAddressHelper header : IpAddressHelper.values())
        {
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
            {
                ip = request.getHeader(header.getKey());
                LOGGER.info("tried:" + header);
            }
        }

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

        return ip;
    }

    public static String getClientIpAddr(Map<String, String> requestHeaders)
    {
        String ip = null;
        for (IpAddressHelper header : IpAddressHelper.values())
        {
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
            {
                ip = requestHeaders.get(header.getKey());
                LOGGER.info("tried:" + header);
            }
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = requestHeaders.get(REMOTE_ADDR);
        }

        return ip;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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