问题
H.e.l.l.o community, i hope someone can help me ... i am using apache tomcat 8.0.0-RC5 and JSR-356 web socket API ... I have 2 questions:
1) Is it possible to get the client ip on @OnOpen method ??
2) Is it possible to get the origin of the connection ???
I followed the websocket example which comes with the distribution of tomcat and i was not able to find the answers .... My java class is basically as follow
@ServerEndpoint(value = "/data.socket")
public class MyWebSocket {
@OnOpen
public void onOpen(Session session) {
// Here is where i need the origin and remote client address
}
@OnClose
public void onClose() {
// disconnection handling
}
@OnMessage
public void onMessage(String message) {
// message handling
}
@OnError
public void onError(Session session, Throwable throwable) {
// Error handling
}
}
回答1:
Repeating the answer I already gave you on the Tomcat users mailing list...
Client IP. No. Generally this type of information is available at the handshake which occurs before OnOpen but client IP is not one of the pieces of information exposed. You might be better blocking these earlier e.g. with iptables or similar.
Origin. ServerEndpointConfig.Configurator.checkOrigin(String)
You'll need a custom Configurator. Keep in mind that a malicious client can forge the origin header.
来源:https://stackoverflow.com/questions/19624012/apache-tomcat-8-websocket-origin-and-client-address