此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Session 3.3.1! |
此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Session 3.3.1! |
Spring Session 提供与 Spring 的 WebSocket 支持的透明集成。
Spring Session 的 WebSocket 支持仅适用于 Spring 的 WebSocket 支持。 具体来说,它不能直接使用 JSR-356,因为 JSR-356 没有拦截传入 WebSocket 消息的机制。 |
Spring Session 的 WebSocket 支持仅适用于 Spring 的 WebSocket 支持。 具体来说,它不能直接使用 JSR-356,因为 JSR-356 没有拦截传入 WebSocket 消息的机制。 |
为什么选择 Spring Session 和 WebSockets?
那么,当我们使用 WebSocket 时,为什么我们需要 Spring Session?
考虑一个电子邮件应用程序,它通过 HTTP 请求完成大部分工作。
但是,其中还嵌入了一个聊天应用程序,该应用程序可通过 WebSocket API 工作。
如果用户正在积极与某人聊天,我们不应该超时,因为这将是一个非常糟糕的用户体验。
然而,这正是 JSR-356 所做的。HttpSession
另一个问题是,根据 JSR-356,如果超时,任何用它创建的 WebSocket 和经过身份验证的用户都应该被强制关闭。
这意味着,如果我们在应用程序中主动聊天并且不使用 HttpSession,我们也会断开与对话的连接。HttpSession
HttpSession
WebSocket 用法
WebSocket 示例提供了如何将 Spring Session 与 WebSocket 集成的工作示例。 您可以按照以下几个标题中描述的基本集成步骤进行操作,但我们建议您在与自己的应用程序集成时遵循详细的 WebSocket 指南。
HttpSession
集成
在使用 WebSocket 集成之前,应确保先进行 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 | 我们不是实现,而是扩展WebSocketMessageBrokerConfigurer AbstractSessionWebSocketMessageBrokerConfigurer |
2 | 我们将该方法重命名为registerStompEndpoints configureStompEndpoints |
幕后做什么?AbstractSessionWebSocketMessageBrokerConfigurer
-
WebSocketConnectHandlerDecoratorFactory
作为 添加到 中。 这可确保触发包含 . 这是结束 Spring 会话结束时仍处于打开状态的任何 WebSocket 连接所必需的。WebSocketHandlerDecoratorFactory
WebSocketTransportRegistration
SessionConnectEvent
WebSocketSession
WebSocketSession
-
SessionRepositoryMessageInterceptor
作为 a 添加到每个 . 这可确保将 添加到 WebSocket 属性中,以便能够更新上次访问的时间。HandshakeInterceptor
StompWebSocketEndpointRegistration
Session
-
SessionRepositoryMessageInterceptor
作为 a 添加到我们的入站 . 这可确保每次收到入站消息时,都会更新春季会话的上次访问时间。ChannelInterceptor
ChannelRegistration
-
WebSocketRegistryListener
被创建为 Spring Bean。 这可确保我们将所有 ID 映射到相应的 WebSocket 连接。 通过维护此映射,我们可以在 Spring Session (HttpSession) 结束时关闭所有 WebSocket 连接。Session
1 | 我们不是实现,而是扩展WebSocketMessageBrokerConfigurer AbstractSessionWebSocketMessageBrokerConfigurer |
2 | 我们将该方法重命名为registerStompEndpoints configureStompEndpoints |