How do I get the remote address of a client in ser

2018-12-31 20:49发布

问题:

Is there any way that I could get the original IP address of the client coming to the server? I can use request.getRemoteAddr(), but I always seem to get the IP of the proxy or the web server.

I would want to know the IP address that the client is using to connect to me. Is there anyway that I could get it?

回答1:

try this:

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;  
    }  


回答2:

request.getRemoteAddr() is the way. It appears your proxy changes the source IP. When some proxies do that they add the original IP in some custom http header. Use request.getHeaderNames() and request.getHeaders(name) and print all of them to see if there isn\'t anything of interest. Like X-CLIENT-IP (made that one up, but they look like this)



回答3:

The best solution I\'ve ever used

public 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;      
} 


回答4:

You cannot do this in a meaningful way.

The proxy may or may not add a proxied-for header, but in many cases this will be an internal only address anyway, so it will be meaningless to you. Most proxies at the edge of an organization are configured to reveal as little as possible about the internals of the network anyway.

What are you intending to use this information for?



回答5:

As this is usually a deployment concern, rather than an application concern, another approach would be to configure the application container appropriately. Once configured, the container takes care of inspecting the appropriate header and your application continues to use request.getRemoteAddr().

For example, in Tomcat you can use the Remote IP Valve. I would assume most application servers have similar functionality.

The container could also take care of understanding if your front-end load balancer is terminating SSL connections, forwarding the request to the app server over HTTP. This is important when your application needs to generate URLs to itself.



回答6:

\"x-forwarded-for\" request header contains the original client IP if using a proxy or a load balancer. But I think not all proxies/lb adds this header.

Here some java code to parse the header: http://www.codereye.com/2010/01/get-real-ip-from-request-in-java.html

If this header is not present then I would proceed as @Bozho suggests



回答7:

String ipAddress = request.getHeader(\"x-forwarded-for\");
        if (ipAddress == null) {
            ipAddress = request.getHeader(\"X_FORWARDED_FOR\");
            if (ipAddress == null){
                ipAddress = request.getRemoteAddr();
            }
        }


回答8:

InetAddress inetAddress = InetAddress.getLocalHost();
String ip = inetAddress.getHostAddress();