如何解决如何在Spring Websockets + STOMP中拒绝服务器端的连接并释放所有资源?
我们有一个使用STOMP(Web套接字)运行的Java(春季启动)应用程序。客户端(JavaScript,SoсkJS)连接到它。 另外,在服务器上有两个级别,我们可以在这些级别上对用户进行身份验证,并使用无效的数字签名终止连接。
这些是服务器端的两个拦截器:
- http级(我们可以终止连接,但是在此级别上,尚没有包含签名的STOMP标头。
- 重击级别。我们有所有的头条新闻,但是没有明显的机会让客户断开连接。
@Configuration
@EnableWebSocketMessageBroker
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
// HTTP-level handshake interceptor (1st step)
// Here we accessing all HTTP headers,and can using them for autentication
// But not STOMP message headers,because STOMP not working at this point
// We can disconnect a client at the moment when we return false in the interceptor method
registry.addEndpoint(ENDPOINT_URI_PATH).withSockJS().setInterceptors(handShakeInterceptor);
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
// STOMP-level interceptor reacting on CONNECT (2nd step)
// Here we accessing all headers,HTTP,STOMP,etc
// But in this interceptor,we cannot disconnect the client when we return `null` in the preSend method
// or throw an exception. In this case,the connection sessions will be
// closed (as shown in the Java code),but on my operating system I can see the established connections.
// When there are many such attempts,free ports run out,and the
// application generally stops creating new connections.
registration.interceptors(preSendInterceptor);
}
}
因此,当我们在preSendInterceptor null
的{{1}}方法中返回preSend
并通过stompSession.isConnected()检查客户端状态时,它返回false
另外,我们无法发送或接收消息。
但是,命令的输出
ChannelInterceptor
是
指示仍在建立连接。 我离开测试很长时间了,增加了新的客户端,此后又拒绝了服务,因为显然没有免费的端口。
P.S。 客户端尝试从Javascript连接时,只能将STOMP标头发送到特定的URL。 作为解决方案之一,我们建议使用URL查询参数将签名转移到autenticate。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。