How do I get the remote address of a client in servlet?

前端 未结 10 1918
刺人心
刺人心 2020-11-22 07:32

Is there any way that I could get the original IP address of the client coming to the server? I can use request.getRemoteAddr(), but I always seem to get the IP

10条回答
  •  情话喂你
    2020-11-22 08:14

    As this is usually a deployment concern, rather than an application concern, another approach would be to configure the application container appropriately. Once configured, the container takes care of inspecting the appropriate header and your application continues to use request.getRemoteAddr().

    For example, in Tomcat you can use the Remote IP Valve. I would assume most application servers have similar functionality.

    The container could also take care of understanding if your front-end load balancer is terminating SSL connections, forwarding the request to the app server over HTTP. This is important when your application needs to generate URLs to itself.

提交回复
热议问题