简体   繁体   English

使用java获取客户端计算机的ip地址

[英]Obtaining ip address of client machine using java

I am supposed to capture the ip address of the client machine , who hits my web application. 我应该捕获客户端计算机的IP地址,该计算机命中我的Web应用程序。 After googling, I have found two ways : 谷歌搜索后,我发现了两种方式:

  1. request.getRemoteAddr()

  2. request.getHeader("X-FORWARDED-FOR")

but both of them do not guarantee returning the actual ip of the client machine. 但是他们都不保证返回客户机的实际ip。 Also, I am supposed to achieve the same using struts1.2 , as the application is developed in struts1.2. 另外,我应该使用struts1.2来实现相同的功能,因为应用程序是在struts1.2中开发的。

Is there any struts class for the same? 有同样的struts类吗?

I am using the following code: 我使用以下代码:

String ipAddress = request.getHeader("X-FORWARDED-FOR");  
if (ipAddress == null) {  
    ipAddress = request.getRemoteAddr();  
}

but the method request.getHeader("X-FORWARDED-FOR"); 但方法是request.getHeader("X-FORWARDED-FOR"); returns null when I access the application from a mobile or wireless device. 从移动或无线设备访问应用程序时返回null

As a result, ip is obtained from method request.getRemoteAddr(); 结果,ip从方法request.getRemoteAddr(); The issue is the ip retrieved is private and not public but i want the public ip of client. 问题是检索到的IP是私有的而不是公共的,但我想要客户端的公共IP

request.getRemoteAddr() will return the correct address in most cases (this is part of Java EE not struts). 在大多数情况下, request.getRemoteAddr()将返回正确的地址(这是Java EE的一部分而不是struts)。 They do not guarantee returning the actual IP of the client machine as this is problem of the TCP/IP limitation, not structs or Java itself. 它们不保证返回客户端计算机的实际IP,因为这是TCP / IP限制的问题,而不是结构或Java本身。

The problem is that there's just no way to guarantee it. 问题是,没有办法保证它。 The client might be NAT-ted, so you won't be able to find the address that the client knows in that case anyway, and if the client connects by a proxy (common for certain classes of device, or when the connection is from some locations) then there's no way to guarantee that the proxy will tell you what the client's address is. 客户端可能是NAT-ted,因此无论如何您将无法找到客户端知道的地址,以及客户端是否通过代理连接(对于某些类别的设备是常见的,或者当连接来自某些地方)然后没有办法保证代理会告诉你客户的地址是什么。 There are some horribly broken proxies out there, and even when working correctly they may still be configured to hide the information you are looking for. 有一些可怕的破坏代理,即使工作正常,他们仍然可能被配置为隐藏您正在寻找的信息。

The easiest technique might be to run some javascript on client to discover the information and send it to you in a background request. 最简单的技术可能是在客户端上运行一些javascript以发现信​​息并在后台请求中将其发送给您。 But then you'll discover that the client's address is often not very useful anyway. 但是你会发现客户的地址通常不是很有用。 You surely can't use it for authentication purposes (the widespread prevalence of NAT and dynamic address allocation via DHCP means that you'll often get different clients with the same “internal” address) or connecting back to the client (most clients run a strong firewall or are behind one) and I have no idea why you'd even want to log it. 您肯定无法将其用于身份验证(通过DHCP广泛流行的NAT和动态地址分配意味着您通常会使用相同的“内部”地址获得不同的客户端)或连接回客户端(大多数客户端运行强大的防火墙或落后于一个)我不知道为什么你甚至想要记录它。 It's about the most useless thing you could ask for. 这是你可以要求的最无用的东西。 I advise taking a step back from this problem and to think again about what you're really trying to do. 我建议从这个问题上退后一步,再考虑一下你真正要做的事情。

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

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