如何从http请求获取子网IP地址 [英] How to get the subnet ip adress from http request

查看:153
本文介绍了如何从http请求获取子网IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Web应用程序,我需要为应用程序中的所有操作执行审核日志。为此,我需要获取客户端系统的IP地址。

I'm writing a web application, I need to do a audit log for all the actions in the application. For this purpose I need to get the IP Address of the client systems.

我正在使用request.getRemoteAddr()来获取远程IP地址。但这有一个问题,如果客户端在代理后面,这个方法将给出代理系统的IP。

I'm using request.getRemoteAddr() to get the remote IP Address. But this has a problem, if the client is behind a proxy this method will give the IP of the proxy system.

当我进行一些搜索时,我发现了一个名为title的属性HttpRequest对象中的'X-FORWARDED-FOR'。

When I did some search I found a header attribute called 'X-FORWARDED-FOR' in the HttpRequest object.

有人可以告诉我这个头属性是如何工作的,我怎样才能使用这个头来获取IP地址客户系统。

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.

谢谢

推荐答案

getRemoteIP 返回用户的远程IP地址(假设所有HTTP中介都是表现良好的XFF标题)。

getRemoteIP returns the remote IP address of the user (assuming all HTTP intermediaries are well behaved wrt XFF header).

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

这篇关于如何从http请求获取子网IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆