如何在Spring Websockets + STOMP中拒绝服务器端的连接并释放所有资源?

问题描述

我们有一个使用STOMP(Web套接字)运行的Java(春季启动)应用程序。客户端(JavaScript,SoсkJS)连接到它。 另外,在服务器上有两个级别,我们可以在这些级别上对用户进行身份验证,并使用无效的数字签名终止连接。

这些是服务器端的两个拦截器:

  1. http级(我们可以终止连接,但是在此级别上,尚没有包含签名的STOMP标头。
  2. 重击级别。我们有所有的头条新闻,但是没有明显的机会让客户断开连接。
@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

ss -natu output

指示仍在建立连接。 我离开测试很长时间了,增加了新的客户端,此后又拒绝了服务,因为显然没有免费的端口。

P.S。 客户端尝试从Javascript连接时,只能将STOMP标头发送到特定的URL。 作为解决方案之一,我们建议使用URL查询参数将签名转移到autenticate。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)