2

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

Alan
  • 574
  • 2
  • 9
  • 16
  • Do you have a proxy or another web server (Apache, etc) in front of your application server? Have you tried accessing the server remotely and do you get the same result? – Boden Apr 29 '09 at 21:55

2 Answers2

3

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
  • 302,674
  • 57
  • 556
  • 614
  • 2
    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.
    X-Forwarded-For: client1, proxy1, proxy2
    
    – Trey Apr 29 '09 at 23:08
  • @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 Apr 30 '09 at 21:53
2

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

cleclerc@xebia.fr

(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