简体   繁体   English

如何从http请求获取子网ip地址

[英]How to get the subnet ip adress from http request

I'm writing a web application, I need to do a audit log for all the actions in the application. 我正在编写一个Web应用程序,我需要对应用程序中的所有操作进行审核日志。 For this purpose I need to get the IP Address of the client systems. 为此,我需要获取客户端系统的IP地址。

I'm using request.getRemoteAddr() to get the remote IP Address. 我正在使用request.getRemoteAddr()来获取远程IP地址。 But this has a problem, if the client is behind a proxy this method will give the IP of the proxy system. 但这有一个问题,如果客户端位于代理之后,则此方法将提供代理系统的IP。

When I did some search I found a header attribute called 'X-FORWARDED-FOR' in the HttpRequest object. 当我进行一些搜索时,我在HttpRequest对象中发现了一个名为“ X-FORWARDED-FOR”的标头属性。

Can somebody tell me how exactly this header property works and how can I used this header to get the IP address of the client system. 有人可以告诉我此标头属性的工作原理以及如何使用此标头获取客户端系统的IP地址。

Thank you 谢谢

getRemoteIP returns the remote IP address of the user (assuming all HTTP intermediaries are well behaved wrt XFF header). getRemoteIP返回用户的远程IP地址(假设所有HTTP中介均具有XFF标头的行为)。

String getRemoteIP(HttpServletRequest request) {
    String xff = request.getHeader("X-Forwarded-For");
    if (xff != null) {
        return xff.split("[\\s,]+")[0];
    }
    return request.getRemoteAddr();
}

客户端的代理(通常是防火墙等)将使用从客户端接收到的ip填充x-forward-for标头,通常这是(但不是必须的)(在用户经过多个代理或防火墙)用户计算机的IP。

'X-FORWARDED-FOR' is used for identifying the originating/actual IP address of a client connecting to a web server through an HTTP proxy. “ X-FORWARDED-FOR”用于标识通过HTTP代理连接到Web服务器的客户端的始发/实际IP地址。

You can simply use the value for this attribute to find out the originating client IP, even if it's behind a proxy. 您可以简单地使用此属性的值来查找原始客户端IP,即使它位于代理之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM