tags:

views:

511

answers:

3

While this may seem simple, i have tried the usual request.getRemoteAddr(), request.getRemoteHost() but i keep getting my servers address. Something about my configuration, im getting my gateway ip. Does anyone know how to get the acutal users IP address?

thanks

+2  A: 

If there's some proxy between you and the user, then you might have to look at the X-Forwarded-For header. Note that this isn't guaranteed to work either, but if you only need "good enough", then it might suffice.

Joachim Sauer
A warning about X-Forwarded-For. Some implementations will actually add a list of IP Addresses that may be in the chain, so you might need to anticipate which one was the original address.<pre>X-Forwarded-For: client1, proxy1, proxy2</pre>
Trey
@Trey: thanks for the comment re: IP Address chain. I have to modify some logging code to use X-Forwarded-For and now I know to anticipate this possiblity, rather than being surprised by it.
Grant Wagner
+1  A: 

Apache Http will integrate a secure mechanism to handle X-Forwarded-For header with mod_remoteip (1).

Here are a Tomcat valve RemoteIpValve (2) and a servlet filter XForwardedFilter (3) to integrate the X-Forwarded-For and X-Forwarded-Proto headers respectively at the Tomcat and WAR levels with the same secure mechanism as mod_remoteip does.

Thanks to this, request.getRemoteAddr(), request.getRemoteHost(), request.isSecure(), request.getScheme() and request.getServerPort() will expose the values transmitted by X-Forwarded-For and X-Forwarded-Proto rather than the values of the preceding proxy / load balancer.

Hope this helps,

Cyrille

[email protected]

(1) http : / / httpd.apache.org/docs/trunk/mod/mod_remoteip.html

(2) http : / / code.google.com/p/xebia-france/wiki/RemoteIpValve

(3) http://code.google.com/p/xebia-france/wiki/XForwardedFilter

A: 

Dear all, here is an update :

[RemoteIpValve has been integrated in Tomcat 6.0.24][1] and [RemoteIpFilter in Tomcat 7.0.0][2]. Here is a detailed article in French : [Tomcat : Adresse IP de l’internaute, load balancer, reverse proxy et header Http X-Forwarded-For][3] (sorry, we didn't have the time to translate this article in English).

Cyrille (Xebia)

[1] http:// tomcat.apache.org/tomcat-7.0-doc/config/valve.html#Remote_IP_Valve

[2] http:// tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Remote_IP_Filter

[3] http://blog.xebia.fr/2009/05/05/tomcat-adresse-ip-de-linternaute-load-balancer-reverse-proxy-et-header-http-x-forwarded-for/

Cyrille Le Clerc