Spring Session - WebSocket
HttpSession 设置
第一步是将 Spring Session 与 HttpSession 集成。这些步骤已在使用 Redis 的 HttpSession 指南中概述。
请确保在继续之前已将 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 支持,我们只需更改两件事
1 | 我们扩展 AbstractSessionWebSocketMessageBrokerConfigurer ,而不是实现 WebSocketMessageBrokerConfigurer |
2 | 我们将 registerStompEndpoints 方法重命名为 configureStompEndpoints |
AbstractSessionWebSocketMessageBrokerConfigurer
在幕后做了什么?
-
WebSocketConnectHandlerDecoratorFactory
作为WebSocketHandlerDecoratorFactory
添加到WebSocketTransportRegistration
。这确保触发一个包含WebSocketSession
的自定义SessionConnectEvent
。当 Spring Session 结束时,需要WebSocketSession
来终止所有仍处于打开状态的 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 与 WebSockets 一起使用。
运行 websocket
示例应用
您可以通过获取源代码并执行以下命令来运行示例
$ ./gradlew :spring-session-sample-boot-websocket:bootRun
为了测试会话过期,您可以在启动应用之前添加以下配置属性,将会话过期时间改为 1 分钟(默认为 30 分钟) src/main/resources/application.properties
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
现在点击登录按钮。您现在应该已以用户 rob 的身份通过身份验证。
打开隐身窗口并访问 localhost:8080/
系统会提示您填写登录表单。使用以下信息进行身份验证
-
用户名 luke
-
密码 password
现在从 rob 发送消息给 luke。消息应该会显示。
等待两分钟,然后再次尝试从 rob 发送消息给 luke。您会看到消息不再发送。
为什么是两分钟?
Spring Session 在 60 秒内过期,但无法保证在 60 秒内收到 Redis 的通知。为了确保套接字在合理的时间内关闭,Spring Session 每分钟的 00 秒会运行一个后台任务,强制清理所有过期的会话。这意味着您最多需要等待两分钟,WebSocket 连接才会关闭。 |
您现在可以尝试访问 localhost:8080/。系统会再次提示您进行身份验证。这表明会话已正常过期。
现在重复相同的练习,但不是等待两分钟,而是每隔 30 秒从每个用户发送一条消息。您会看到消息持续发送。尝试访问 localhost:8080/。系统不会再次提示您进行身份验证。这表明会话保持活动状态。
只有用户发送的消息才能保持会话活动。这是因为只有来自用户的消息才表示用户活动。接收到的消息不表示活动,因此不会更新会话过期时间。 |