Is it possible to accurately determine the IP address of a client in java servlet

后端 未结 5 2021
情歌与酒
情歌与酒 2020-12-03 16:13

I want to configure a machine in my network to accept all calls from a specific machine without authentication. For this I am planning to use the IP address of the client ma

5条回答
  •  一整个雨季
    2020-12-03 16:28

    You can use the getRemoteAddr() method from the HttpServletRequest class to obtain the IP address. Be careful, though. If your client is behind a proxy server (or even a NATting firewall), you'll get the proxy IP address instead.

    So, you can also look for the X-Forwarded-For HTTP header (standard for identifying the source IP address of a client behind an HTTP proxy). See more on Wikipedia. Be careful, though. If your client is NOT behind a proxy, you can get a null XFF header. So, if you are to follow this path, you should use a mix of the servlet methods and XFF header evaluation. There is no guarantee, though, that the proxy will forward you the header.

    But be aware that the source IP address can be easily changed or faked by any malicious client. I really recommend using some sort of client authentication (a certificate, for example). There is no way for a web app to accurately determine the client IP address.

提交回复
热议问题