Spring 会话 - WebSocket
HttpSession 设置
第一步是将 Spring Session 与 HttpSession 集成。这些步骤已在 HttpSession with 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");
}
}
要 hook Spring Session 支持,我们只需要改变两件事:
1 | 而不是实施WebSocketMessageBrokerConfigurer ,我们扩展AbstractSessionWebSocketMessageBrokerConfigurer |
2 | 我们将registerStompEndpoints method 设置为configureStompEndpoints |
什么AbstractSessionWebSocketMessageBrokerConfigurer
做幕后工作?
-
WebSocketConnectHandlerDecoratorFactory
添加为WebSocketHandlerDecoratorFactory
自WebSocketTransportRegistration
. 这可确保自定义SessionConnectEvent
触发的WebSocketSession
. 这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 与 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 在本地主机上运行 Redis。
有关详细说明,请参阅 Docker Redis 存储库。 |
您现在应该能够访问 localhost:8080/ 中的应用程序
探索websocket
示例应用程序
现在您可以尝试使用该应用程序。使用以下信息进行身份验证:
-
用户名 rob
-
密码 密码
现在单击 Login 按钮。现在,您应该以用户 rob.
打开一个隐身窗口并访问 localhost:8080/
系统将提示您输入登录表单。使用以下信息进行身份验证:
-
用户名 luke
-
密码 密码
现在从 rob 向 luke 发送消息。此时应显示该消息。
等待 2 分钟,然后再次尝试从 rob 向 luke 发送消息。 您可以看到该消息不再发送。
为什么是 2 分钟?
Spring Session 将在 60 秒后过期,但不能保证在 60 秒内收到来自 Redis 的通知。 为了确保套接字在合理的时间内关闭,Spring Session 每分钟在 00 秒运行一个后台任务,强制清理任何过期的会话。 这意味着您最多需要等待两分钟,然后 WebSocket 连接才会关闭。 |
您现在可以尝试访问 localhost:8080/系统会提示您再次进行身份验证。 这表明会话已正确过期。
现在重复相同的练习,但不要等待两分钟,而是每 30 秒向每个用户发送一条消息。 您可以看到消息继续发送。 尝试访问 localhost:8080/系统不会提示您再次进行身份验证。 这表明会话保持活动状态。
只有从用户发送的消息才能使会话保持活动状态。 这是因为只有来自用户的消息才暗示用户活动。 收到的消息并不意味着活动,因此不会续订会话过期。 |