Get client IP address in JSP

I need to get the client IP address on the JSP page. I tried the following methods:

request.getRemoteAddr() request.getHeader("X_FORWARDED_FOR") request.getHeader("HTTP_CLIENT_IP") request.getHeader("WL-Proxy-Client-IP") request.getHeader("Proxy-Client-IP") request.getHeader("REMOTE_ADDR") 

However, none of these methods returned the desired IP address. How to get client IP address on JSP page?

+6
source share
4 answers
 <% out.print( request.getRemoteAddr() ); out.print( request.getRemoteHost() ); %> 

You cannot get the real IP address of the client, if the client is behind the proxy server, you will get the IP address of the proxy server, not the client. However, the proxy server may include the requesting IP address of the client in a special HTTP header.

 <% out.print( request.getHeader("x-forwarded-for") ); %> 
+3
source

Is your application server behind a load balancer, proxy, or web server? Just an example; The F5 load balancer provides the client IP address to the rlnclientipaddr header:

 request.getHeader("rlnclientipaddr"); 
+3
source

To get the client IP address, I used the following method

 <% String ip = request.getHeader("X-Forwarded-For"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } %> 

Hope this helps, please leave feedback.

+2
source

Are you using a reverse proxy server like apache proxy? http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

When working in reverse proxy mode (for example, using the ProxyPass directive) mod_proxy_http adds several request headers to transfer information to the source server. These headers:

 X-Forwarded-For The IP address of the client. X-Forwarded-Host The original host requested by the client in the Host HTTP request header. X-Forwarded-Server The hostname of the proxy server. 
0
source

Source: https://habr.com/ru/post/921460/


All Articles