如何在java服务器中拒绝某些ip
如果客户端有特定的 IP 地址,有没有办法阻止到 java 服务器的传入连接?
Is there a way to block incoming connections to a java server if the client has a certain ip address?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
接受连接,然后获取该连接的
Socket
。之后,就很简单了,只需调用socket.getInetAddress()
并决定是要回复数据还是关闭套接字。请注意,由于 NAT 的普遍存在,您可能无法真正区分谁正在连接到您,因为请求可能已被重写(通过 NAT),看起来像是来自网关计算机而不是最终客户端。
如果这是尝试提供“稍微更安全”的连接集,请记住,IP 地址只是简单的欺骗,并且其他人将他们的计算机设置为拥有您的 IP 地址并没有真正的障碍。
Accept the connection, then get the
Socket
for that connection. After that, it's a simple as callingsocket.getInetAddress()
and deciding if you want to reply with data or close the socket.Beware, due to the prevalance of NAT, you might not be able to really differentiate who is connecting to you, as the request may have been rewritten (by NAT) to appear to come from a gateway machine instead of an end-client.
If this is an attempt to provide a "slightly more secure" set of connections, remember that IP addresses are trivially spoofed, and there are no real barriers from someone else setting their machine to have your IP address.