此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Session 3.3.1

此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Session 3.3.1

本指南介绍如何使用 Spring Session 来确保 WebSocket 消息使 HttpSession 保持活动状态。

Spring Session 的 WebSocket 支持仅适用于 Spring 的 WebSocket 支持。 具体来说,它不能直接使用 JSR-356,因为 JSR-356 没有拦截传入 WebSocket 消息的机制。
Spring Session 的 WebSocket 支持仅适用于 Spring 的 WebSocket 支持。 具体来说,它不能直接使用 JSR-356,因为 JSR-356 没有拦截传入 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 支持。 以下示例演示如何执行此操作:

src/main/java/samples/config/WebSocketConfig.java
@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 我们不是实现,而是扩展WebSocketMessageBrokerConfigurerAbstractSessionWebSocketMessageBrokerConfigurer
2 我们将该方法重命名为registerStompEndpointsconfigureStompEndpoints

幕后做什么?AbstractSessionWebSocketMessageBrokerConfigurer

  • WebSocketConnectHandlerDecoratorFactory作为 添加到 中。 这可确保触发包含 . 这是结束 Spring 会话结束时仍处于打开状态的任何 WebSocket 连接所必需的。WebSocketHandlerDecoratorFactoryWebSocketTransportRegistrationSessionConnectEventWebSocketSessionWebSocketSession

  • SessionRepositoryMessageInterceptor作为 a 添加到每个 . 这可确保将 添加到 WebSocket 属性中,以便能够更新上次访问的时间。HandshakeInterceptorStompWebSocketEndpointRegistrationSession

  • SessionRepositoryMessageInterceptor作为 a 添加到我们的入站 . 这可确保每次收到入站消息时,都会更新春季会话的上次访问时间。ChannelInterceptorChannelRegistration

  • WebSocketRegistryListener被创建为 Spring Bean。 这可确保我们将所有 ID 映射到相应的 WebSocket 连接。 通过维护此映射,我们可以在 Spring Session (HttpSession) 结束时关闭所有 WebSocket 连接。Session

1 我们不是实现,而是扩展WebSocketMessageBrokerConfigurerAbstractSessionWebSocketMessageBrokerConfigurer
2 我们将该方法重命名为registerStompEndpointsconfigureStompEndpoints

websocket示例应用程序

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

运行示例应用程序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) 运行它。 或者,您可以将指向 Redis 服务器更新。 另一种选择是使用 Docker 在 localhost 上运行 Redis。 有关详细说明,请参阅 Docker Redis 存储库RedisConnectionFactory

您现在应该能够在 localhost:8080/ 访问该应用程序

探索示例应用程序websocket

现在您可以尝试使用该应用程序。使用以下信息进行身份验证:

  • 用户名 rob

  • 密码 密码

现在点击 登录 按钮。现在,您应该被身份验证为用户 rob

打开隐身窗口并访问 localhost:8080/

系统会提示您输入登录表单。使用以下信息进行身份验证:

  • 用户名 luke

  • 密码 密码

现在从 rob 向 luke 发送一条消息。此时应显示该消息。

等待两分钟,然后再次尝试从 rob 向 luke 发送消息。 您可以看到该消息不再发送。

为什么是两分钟?

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

您现在可以尝试访问 localhost:8080/ 系统会提示您再次进行身份验证。 这表明会话已正确过期。

现在重复相同的练习,但不是等待两分钟,而是每 30 秒从每个用户发送一条消息。 您可以看到消息继续发送。 尝试访问 localhost:8080/ 系统不会提示您再次进行身份验证。 这表明会话保持活动状态。

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

为了测试会话过期时间,您可能希望在启动应用程序之前添加以下配置属性,将会话过期时间更改为 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) 运行它。 或者,您可以将指向 Redis 服务器更新。 另一种选择是使用 Docker 在 localhost 上运行 Redis。 有关详细说明,请参阅 Docker Redis 存储库RedisConnectionFactory
为什么是两分钟?

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

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