Get IP address of client machine in Spring MVC

Viewed 6679

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.

1 Answers
Related