how to get users ip address in java

前端 未结 2 915
忘了有多久
忘了有多久 2021-01-15 00:38

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,

相关标签:
2条回答
  • 2021-01-15 01:22

    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.

    0 讨论(0)
  • 2021-01-15 01:26

    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

    0 讨论(0)
提交回复
热议问题