从服务器端获取最终用户IP地址的可靠方法是什么?
我在服务器端使用 JSP 并想验证用户不能从两个不同的 IP 地址登录。进行此验证的方法是什么?
还有人说,由于涉及到一些代理,可能无法从服务器端获取客户端IP地址。那么谷歌和脸书是如何做到这一点的呢?这在生产环境中可靠吗?请解释一下。谢谢 !
I use JSP in server side and want to validate that, an user must not log in from two different IP addresses. What is the method to do this validation?
And some says that the client IP address may not be get from the server side because of some proxies involved. Then how the google and facebook are doing this? Will this be reliable in production environmennt? please explain. Thank you !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
他们可能使用许多代理服务器的 X-Forwarded-For 标头在途中添加到请求中。
这仅在代理人说真话的情况下才可靠。
They are probably using the X-Forwarded-For header that a lot of proxy servers add to the request on the way through.
This is only reliable to the extent that the proxies are telling the truth.
好吧,获取 IP 地址就像使用 ServletRequest#getRemoteAddr()
但正如您已经注意到的,没有办法可靠地获得此信息 - 如果客户端使用代理,则连接将显示为源自该 IP 地址。我认为 Facebook 或 Google 也无法解决这个问题 - 这就是为什么您可以通过具有美国 IP 地址的代理/ssh 隧道登录来访问仅限美国的功能(例如 Gmail 中的 Google 语音)。
如果您只想阻止用户同时从两个不同的 IP 地址登录,您所需要做的就是跟踪他们当前会话(如果存在)源自哪个 IP 地址,然后
不确定阻止在不同时间从不同 IP 地址登录有什么价值,因为这种情况很可能发生在四处旅行或从一个网络移动到另一个网络的用户身上。
Well, getting the IP address is as easy as using ServletRequest#getRemoteAddr()
But as you've noted already, there is no way to get this reliably - if the client is using a proxy, the connection will appear to originate at that IP address. I don't believe Facebook or Google can get around this either - which is why you can access US only features (for example, Google Voice in Gmail) by logging in via a proxy/ssh tunnel that has a US IP address.
If you just want to stop a user from logging in from two different IP addresses simultaneously, all you need to do is track what IP address their current session (if one exists) originates from and either
I'm not sure what value there might be in preventing a login from different IP addresses at different times since that's very likely going to happen with users who are travelling around or moving from network to network.