1

I'm trying to get client public ip address via servlet as below:

        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        System.out.println("1. ip: "+ip);
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        System.out.println("2. ip: "+ip);
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        System.out.println("3. ip: "+ip);
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        System.out.println("4. ip: "+ip);
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        System.out.println("4. ip: "+ip);

but this only able to return the localhost IP, as you see in below output:

1. ip: null
2. ip: null
3. ip: null
4. ip: null
4. ip: 127.0.0.1

Any help?

tokhi
  • 21,044
  • 23
  • 95
  • 105

2 Answers2

2

It depends from proxy server/load balancer and their configurations. In most cases it is possible to get IP by

request.getHeader("x-forwarded-for") 

or

request.getHeader("x-real-ip")

In case when before your server you have proxy server, make sure that proxy pass headers to your server, e.g. in case of nginx it should look like

server {
  server_name  domain.com;
  ...
  location /path-to-server {
    ...
    proxy_pass  localhost:8080;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    ...
  }
olexii
  • 333
  • 1
  • 8
  • This only returns `null` value – tokhi Sep 02 '13 at 10:59
  • which proxy server you have before your server, e.g. nginx? if nginx make sure that it pass all headers to your server. – olexii Sep 02 '13 at 11:20
  • did nginx pass all headers? can you try enable access log in nginx and check client ip, then in your server dump whole request header and check what is inside. – olexii Sep 02 '13 at 12:03
  • actually I can see that the `tomcat` is not able to send the header to the app.. when I test that with `curl` I get a null header – tokhi Sep 02 '13 at 14:33
0

I do like this,you can have a try

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;      
} 
Ali Hashemi
  • 3,158
  • 3
  • 34
  • 48