1

I tried this code below

@Autowired
private HttpServletRequest request;
request.getRemoteAddr();

but i only get the ip adress of the server of the test environment which is: 220.5.13.85, my ip address is 220.5.13.45.

I tried to view the request headers in pagesource under network tab but i can't find my ip address there. I can only see the Remote Address:220.5.13.85:80 under General and Host:220.5.13.85 Referer:http://220.5.13.85 under request header is the same.

LogronJ
  • 561
  • 2
  • 4
  • 24

1 Answers1

1

There is no standard way of getting the client's ip address.

One way is to parse "X-Forwarded-For" and rely on request.getRemoteAddr();

public static String getClientIpAddress(HttpServletRequest request) {
    String xForwardedForHeader = request.getHeader("X-Forwarded-For");
    if (xForwardedForHeader == null) {
        return request.getRemoteAddr();
    } else {
        return new StringTokenizer(xForwardedForHeader, ",").nextToken().trim();
    }
}

There are other ways also mentioned on the following url:

https://www.mkyong.com/java/how-to-get-client-ip-address-in-java/

AmanSinghal
  • 2,404
  • 21
  • 22