【问题标题】:How to disconnect users' websocket connection from server when the JWT access token expires using Spring Boot + Websockets?当使用 Spring Boot + Websockets 的 JWT 访问令牌过期时,如何断开用户的 websocket 连接?
【发布时间】:2022-08-04 12:34:18
【问题描述】:

现在的情况

我有一个使用 Spring Boot 开发的 Web 应用程序(带有 JWT 令牌身份验证的 REST API)。我使用 Spring Websockets 用 RabbitMQ 实现了一个 STOMP webscoket 服务器。我有一个单独的 React 前端,它使用 REST 端点和 websocket。

当我从前端连接到 websocket 时,我将 JWT 访问令牌作为查询参数传递,如果身份验证成功,则建立 websocket 连接。我使用这个 websocket 连接来传递一些消息仅从服务器到客户端(使用队列)。

问题

问题是,即使在访问令牌过期后,websocket 连接仍然保持活动状态,这是一个严重的安全问题.我想要一种在令牌过期时从服务器端关闭用户连接的方法。不幸的是,我找不到任何示例或机制来处理这种情况。

我的想法

  1. 尝试为每个 Web 会话保持某种到期时间。如果用户在当前访问令牌到期之前获得了新的访问令牌,请延长到期时间。当到期时间到达时,关闭来自服务器的连接。这甚至可能吗?

    有人可以给我一个解决这个问题的方法吗?

    我没有添加任何代码,因为我不确定要在此处添加哪些代码。

  • 你有没有找到解决这个问题的方法?
  • @ruckc 是的,我找到了一个可行的解决方案,即使它不是那么漂亮。让我上传答案。

标签: spring-boot websocket jwt spring-websocket


【解决方案1】:

我找到了解决问题的方法(尽管它不是那么漂亮),效果很好。

诀窍是将 WebSocket 连接对象(在用户连接时生成)存储在存储中(我使用了 ConcurrentHashMap),以及它的到期时间。然后继续检查是否有任何过期的会话并断开它们。

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;
import org.springframework.web.socket.handler.WebSocketHandlerDecorator;
import org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory;
//import org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {


    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config){
        
    }


    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
        WebSocketMessageBrokerConfigurer.super.configureWebSocketTransport(registry);

        registry.addDecoratorFactory(new WebSocketHandlerDecoratorFactory() {
            @Override
            public WebSocketHandler decorate(WebSocketHandler webSocketHandler) {
                return new WebSocketHandlerDecorator(webSocketHandler) {
                    @Override
                    public void afterConnectionEstablished(final WebSocketSession session) throws Exception {
                        try{
                            //Access to the WebSocketSession object

                            //You can access Principal object, JWT token details like expiry time as well.
        
                            //Store the WebSocketSession session in a store alongside expiry time (In a HashMap or any relavent)

                            super.afterConnectionEstablished(session);

                        } catch (Exception e){
                            //Use close method to close the connection at anytime
                            session.close(CloseStatus.BAD_DATA);
                        }
                    }

                    @Override
                    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
                        try{
                            //Remove the WebSocketSession object from the store
                        }catch(Exception e){
                            e.printStackTrace();
                        }finally {
                            super.afterConnectionClosed(session, closeStatus);
                        }


                    }
                };
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 2022-01-14
    • 2017-01-17
    • 2021-08-27
    • 2021-03-03
    • 2019-07-21
    • 2018-06-21
    • 2019-04-10
    • 2019-05-25
    • 1970-01-01
    相关资源
    最近更新 更多