Search code examples
javarestapiip

request.getRemoteAddr() return server ip


@RequestMapping(value = "/origin/test/", method = RequestMethod.GET, headers = "Accept=application/json")
public String getTest(@Context HttpServletRequest request) {
    String Text = "Version - " + org.hibernate.Version.getVersionString();
    return Text+request.getRemoteAddr()+"----"+request.getLocalAddr();
}

In above code when running in the server it return server ip as remote address and also local address. But when running as localhost i have made a request with different laptop it return that laptop IP correctly. But the problem is server not returning the IP of laptop that made the request.


Solution

  • getRemoteAddr() will cause trouble if the client is behind any proxy then you will get the IP address of the proxy

    And when the client is using DHCP then the IP address can and will change during the session.

    getRemoteAddr(): Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.