java Websocket 在连接时获取远程 IP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25913760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Websocket getting remote IP on connect
提问by ZdeenaP
Can I get remote IP address inside @OnOpen
method of @ServletEndpoint
class?
I tried follow this Accessing HttpSession from HttpServletRequest in a Web Socket @SocketEndpointbut as in Websocket - httpSession returns nullit doesn't work.
我可以在类的@OnOpen
方法中获取远程 IP 地址@ServletEndpoint
吗?我尝试按照这个从 HttpServletRequest 中的 Web 套接字 @SocketEndpoint 访问 HttpSession但就像在Websocket 中一样 - httpSession 返回 null它不起作用。
Anyway I only need clients IP address.
无论如何,我只需要客户端的 IP 地址。
采纳答案by Benjamin
The JSR-356 Websocket specification does not expose client IP address.
JSR-356 Websocket 规范不公开客户端 IP 地址。
You may try one of the 2 hacks descibed in this response: JSR-356 WebSockets with Tomcat - How to limit connections within single IP address?
您可以尝试此回复中描述的 2 个 hack 之一: JSR-356 WebSockets with Tomcat - How to limit connections within single IP address?
回答by Aere Xu
Added: Works for Jetty implementation and it's not a common solution. Thanks to Benjamin !
补充:适用于 Jetty 实现,它不是一个常见的解决方案。感谢本杰明!
Don't know if you still need it. I myself directly get the IP from the socket Session.
不知道你是否还需要它。我自己直接从套接字会话中获取IP。
@OnOpen
public void onWebSocketConnect(Session session) {
System.Out.println(session.getUserProperties().get("javax.websocket.endpoint.remoteAddress"))
}
Then I get /127.0.0.1:57045
然后我得到 /127.0.0.1:57045