简体   繁体   中英

Same public ip address is returned in java

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM