跳到主要内容
版本:4.1.1

Spring Session - WebSocket

Doubao 中英对照 Spring Session - WebSocket

本指南描述如何使用 Spring Session 确保 WebSocket 消息让你的 HttpSession 保持活跃。

备注

Spring Session 的 WebSocket 支持仅适用于 Spring 的 WebSocket 支持。具体来说,它不适用于直接使用 JSR-356,因为 JSR-356 没有用于拦截传入 WebSocket 消息的机制。

HttpSession 设置​

第一步是将 Spring Session 与 HttpSession 集成。这些步骤已在 将 HttpSession 与 Redis 结合使用指南 中概述。

在继续之前,请确保你已经将 Spring Session 与 HttpSession 集成。

Spring 配置​

在典型的 Spring WebSocket 应用程序中,你会实现 WebSocketMessageBrokerConfigurer。例如,配置可能大致如下:

@Configuration
@EnableScheduling
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/messages").withSockJS();
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/queue/", "/topic/");
registry.setApplicationDestinationPrefixes("/app");
}

}

我们可以更新配置以使用 Spring Session 的 WebSocket 支持。以下示例展示了如何这样做:

@Configuration
@EnableScheduling
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractSessionWebSocketMessageBrokerConfigurer<Session> { 1

@Override
protected void configureStompEndpoints(StompEndpointRegistry registry) { 2
registry.addEndpoint("/messages").withSockJS();
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/queue/", "/topic/");
registry.setApplicationDestinationPrefixes("/app");
}

}

要接入 Spring Session 支持,我们只需更改两处:

  • 我们不实现 WebSocketMessageBrokerConfigurer,而是扩展 AbstractSessionWebSocketMessageBrokerConfigurer

  • 我们将 registerStompEndpoints 方法重命名为 configureStompEndpoints

AbstractSessionWebSocketMessageBrokerConfigurer 在幕后做了什么?

  • WebSocketConnectHandlerDecoratorFactory 作为 WebSocketHandlerDecoratorFactory 添加到 WebSocketTransportRegistration。这确保触发一个包含 WebSocketSession 的自定义 SessionConnectEvent。WebSocketSession 是必要的,以便在 Spring Session 结束时结束所有仍处于打开状态的 WebSocket 连接。
  • SessionRepositoryMessageInterceptor 作为 HandshakeInterceptor 添加到每个 StompWebSocketEndpointRegistration。这确保将 Session 添加到 WebSocket 属性中,以便更新最后访问时间。
  • SessionRepositoryMessageInterceptor 作为 ChannelInterceptor 添加到我们的入站 ChannelRegistration。这确保每次收到入站消息时,都会更新我们 Spring Session 的最后访问时间。
  • WebSocketRegistryListener 创建为一个 Spring bean。这确保我们拥有所有 Session ID 到对应 WebSocket 连接的映射。通过维护此映射,我们可以在 Spring Session(HttpSession)结束时关闭所有 WebSocket 连接。

websocket 示例应用程序​

websocket 示例应用程序演示了如何将 Spring Session 与 WebSocket 结合使用。

运行 websocket 示例应用程序​

你可以通过获取源代码并运行以下命令来启动示例:

$ ./gradlew :spring-session-sample-boot-websocket:bootRun
提示

为了测试会话过期,你可能希望在启动应用程序之前添加以下配置属性,将会话过期时间改为 1 分钟(默认是 30 分钟):

server.servlet.session.timeout=1m # Session timeout. If a duration suffix is not specified, seconds will be used.
备注

要使示例正常工作,你必须在 localhost 上安装 Redis 2.8+ 并以默认端口(6379)运行。或者,你可以更新 RedisConnectionFactory 以指向 Redis 服务器。另一个选择是使用 Docker 在 localhost 上运行 Redis。详细说明请参阅 Docker Redis 仓库。

现在你应该可以通过 localhost:8080/ 访问该应用程序了。

探索 websocket 示例应用程序​

现在你可以尝试使用该应用程序。使用以下信息认证:

  • 用户名 rob
  • 密码 password

现在点击 Login 按钮。你现在应该已作为用户 rob 认证。

打开一个无痕窗口并访问 localhost:8080/。

系统会提示你输入登录表单。使用以下信息认证:

  • 用户名 luke
  • 密码 password

现在从 rob 向 luke 发送一条消息。消息应该会出现。

等待两分钟,再次尝试从 rob 向 luke 发送消息。你会看到消息不再被发送。

备注

为什么是两分钟?

Spring Session 在 60 秒后过期,但来自 Redis 的通知不保证在 60 秒内发生。为了确保套接字在合理时间内关闭,Spring Session 每分钟在第 00 秒运行一个后台任务,强制清理所有过期会话。这意味着你最多需要等待两分钟,WebSocket 连接才会关闭。

现在你可以尝试访问 localhost:8080/。系统会提示你再次认证。这证明会话正确地过期了。

现在重复同样的练习,但不要等待两分钟,而是每 30 秒从每个用户发送一条消息。你会看到消息继续被发送。尝试访问 localhost:8080/。系统不会提示你再次认证。这证明会话保持活跃。

备注

只有从用户发送的消息才会保持会话活跃。这是因为只有来自用户的消息才意味着用户活动。接收到的消息不意味着活动,因此不会续期会话过期。