title: Get IP address of client machine in Spring MVC
body: <p>I have a <code>java spring MVC</code> 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,</p>
  
  <pre><code>InetAddress addr = InetAddress.getLocalHost();
  String ipAddr = addr.getHostAddress();
  </code></pre>
  
  <p>But this is returning me the IP address of localhost.
  I have also tried few other suggestions found online like the one below:</p>
  
  <pre><code>String remoteAddr = "";
  if (request != null) {
    remoteAddr = request.getHeader("X-FORWARDED-FOR");
    if (remoteAddr == null || "".equals(remoteAddr)) {
        remoteAddr = request.getRemoteAddr();
    }
  }
  </code></pre>
  
  <p>This is also not giving the right IP address. I have also tried the following and nothing has worked for me:
  <a href="https://www.mkyong.com/java/how-to-get-client-ip-address-in-java/" rel="nofollow noreferrer">https://www.mkyong.com/java/how-to-get-client-ip-address-in-java/</a></p>
  
  <p><a href="https://stackoverflow.com/questions/18350318/what-is-the-right-way-to-get-requests-ip">what is the right way to get request&#39;s ip</a></p>
  
  <p>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.</p>