4

I have a java spring MVC web application, I am trying to get the IP address of the client machine who are accessing the application.I had tried the code shown below,

InetAddress addr = InetAddress.getLocalHost();
String ipAddr = addr.getHostAddress();

But this is returning me the IP address of localhost. I have also tried few other suggestions found online like the one below:

String remoteAddr = "";
if (request != null) {
  remoteAddr = request.getHeader("X-FORWARDED-FOR");
  if (remoteAddr == null || "".equals(remoteAddr)) {
      remoteAddr = request.getRemoteAddr();
  }
}

This is also not giving the right IP address. I have also tried the following and nothing has worked for me: https://www.mkyong.com/java/how-to-get-client-ip-address-in-java/

what is the right way to get request's ip

Nothing is giving me the correct IP address. Is there any solution that I could try to get the correct IP address of the machine that is accessing my application.

6
  • 1
    In what way is it wrong? By that I mean what IP do you get? Note that if client is behind a NAT firewall you cannot get the client IP address, just the firewall address.
    – Andreas
    May 16, 2017 at 8:09
  • 1
    you cannot reliably do that. If the client is behind a router doing NAT - like most people are when they are accessing the internet - the best you will get is the router's IP. And when your service is running behind proxys and / or loadbalancers you will only get the client IP if all these components properly set and forward X-FORWARDED-FOR headers. For example in Google-Container-Engine when running on Kubernetes 1.4 there was no chance at all to get the client's IP, as kubernetes was not passing it in. So you can try, but there is no reliable way.
    – P.J.Meisch
    May 16, 2017 at 8:51
  • @Andreas , I am trying to access the application from IP 111.92.68.2. But the IP that is getting printed using the above codes are either 192.168.0.1 or 127.0.0.1 which are not the correct ones
    – user7317496
    May 16, 2017 at 14:31
  • getLocalHost() returns the servers loopback address (127.0.0.1), so that's definitely not it. The other code should have printed the clients IP, but an internal IP address like 192.168.0.1 means your server is likely behind an HTTP proxy or load balancer that doesn't forward that information. But, it might, just using a different name, so try dumping (printing/logging) all the HTTP headers and have a look.
    – Andreas
    May 16, 2017 at 15:58

1 Answer 1

0

You have to read the request from where request is coming, Try this

public void readIp(HttpServletRequest request,HttpServletResponse response){
    String ip = request.getRemoteAddr();
    System.out.println("ip: "+ip);
}
5
  • Did you look at the code in the question? getRemoteAddr() is called inside the inner if statement, and OP said "This is also not giving the right IP address".
    – Andreas
    May 16, 2017 at 8:34
  • @Andreas Do you know from where he is making request to his application, he told that he is getting ip address of local machine, then may be he making request from loacl machine in this case of-course he will get ip address of local machine. Also have you read this code InetAddress addr = InetAddress.getLocalHost(); String ipAddr = addr.getHostAddress(); he is just creating the object of InetAddress, not reading the request, in this case also he will get localhost ip address
    – Satish Kr
    May 16, 2017 at 8:44
  • OP is getting local host IP when using getLocalHost(), but second half of question is using getRemoteAddr(). Please read full question, and notice the line remoteAddr = request.getRemoteAddr();
    – Andreas
    May 16, 2017 at 8:48
  • Can you ask to OP from where he is making request to his appliction???? Because the code is correct it will work anyhow
    – Satish Kr
    May 16, 2017 at 8:51
  • @Satish Kumar, I am trying to access the application from IP 111.92.68.2. But the IP that is getting printed using the above codes are either 192.168.0.1 or 127.0.0.1 which are not the correct ones
    – user7317496
    May 16, 2017 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.