How to get client Ip Address in Java
Normally, you can use servletRequest.getRemoteAddr()
to get the client's IP address that's accessing your Java web application.
String ipAddress = request.getRemoteAddr();
But, if user is behind a proxy server or access your web server through a load balancer (for example, in cloud hosting), the above code will get the IP address of the proxy server or load balancer server, not the original IP address of a client.
To solve it, you should get the IP address of the request's HTTP header "X-Forwarded-For (XFF)".
//is client behind something? String ipAddress = request.getHeader("X-FORWARDED-FOR"); if (ipAddress == null) { ipAddress = request.getRemoteAddr(); }
Read full article from How to get client Ip Address in Java
No comments:
Post a Comment