0

In my project I need to find the public ip address of a machine.

I have implemented that using method mentioned in Finding public ip address using java

I have an api url like 'http://182.14.10.5:8080/test/addVendor' (not exact url) in my project and has been deployed in external tomcat server which has public ip address as 187.15.161.90.

The issue am facing is: When i tried loading this api url from other devices like mobile or computers belong to some other network, am getting the same ip 182.14.161.90 for every URL hit.

This is where i got confused. Whether the program is written according to that or am i getting wrong output.

According to the search results what I understood is, when I load the particular api url, I should get different ip address based on different machine connected with different network.

But am getting the same public ip (182.14.161.90) of the server where my project is deployed.

Can anyone pls clarify it and help me to fix the issue.

Any suggestions would be appreciated.

Thanks

Jeevitha
  • 1
  • 3

1 Answers1

1

It sounds to me like you have a Java Web Application deployed via tomcat and want to get a user's IP address.

The question you've linked in your question Finding public ip address using java is specifically for getting the IP address of the machine where the Java Code is running (for example, if you have a desktop application and want to get the IP of the machine that app is running on for some reason)

If you want to get the user's remote IP, it depends on your server configuration

If Tomcat is the ONLY webserver in your environment, and there is nothing in front of it, you can access the Request's remote ip using HttpServletRequest#getRomoteAddr, which will give you the IP address of the socket connection that initiated the Request.

This will work great unless you have a proxy server, like Apache HTTPD or Nginx in front of Tomcat, in which case you'll need to configure your server to send the X-Forwarded-For header, and get the user's remote IP using HttpServletRequest#getHeader for example, request.getHeader("X-Forwarded-For")

If I've misunderstood your question, please clarify and let me know with a comment.

Zachary Craig
  • 2,192
  • 4
  • 23
  • 34
  • i can get the remote address from httpservletrequest as you said. But what i want is, i need to get the public ip of a device on which my api url is loaded.. i.e: for example if you load http://182.14.10.5:8080/test/addVendor this url from ur desktop or mobile corresponding device's public ip address should be returned. But i am getting the public ip of the server (182.14.10.5) where my project is deployed. – Jeevitha Jul 27 '18 at 12:45
  • So the intention is that test/addVendor is to get the remote IP of the user who requests it? – Zachary Craig Jul 27 '18 at 13:50
  • yes @zack6849, i want to get the corresponding machine's public ip address where the url is loaded from. But instead am getting the same public ip of the server on which my " test/addVendor " is deployed for all the url hits. – Jeevitha Jul 28 '18 at 04:01