# WebSession 集成
Spring Session 提供了与 Spring WebFlux 的WebSession
的透明集成。这意味着你可以使用 Spring Session 支持的实现来切换WebSession
实现。
# 为什么要进行会话和 WebSession?
我们已经提到, Spring Session 提供了与 Spring WebFlux 的WebSession
的透明集成,但是我们从中得到了什么好处呢?与HttpSession
一样, Spring Session 使得在不绑定到特定于应用程序容器的解决方案的情况下支持群集会话变得非常简单。
# 与 Redis 的 WebSession
使用 Spring session withWebSession
是通过注册一个WebSessionManager
实现来启用的,该实现由 Spring session 的ReactiveSessionRepository
支持。 Spring 配置负责创建一个WebSessionManager
,该实现用 Spring Session 支持的实现替换WebSession
实现。要做到这一点,请添加以下 Spring 配置:
@EnableRedisWebSession (1)
public class SessionConfiguration {
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(); (2)
}
}
1 | @EnableRedisWebSession 注释创建了一个名为webSessionManager 的 Spring Bean。这个 Bean 实现了WebSessionManager 。这就是负责替换要由 Spring Session 支持的 WebSession 实现的内容。在这个实例中, Spring Session 是由 Redis 支持的。 |
---|---|
2 | 我们创建一个RedisConnectionFactory 将 Spring Session 连接到 Redis 服务器。我们将连接配置为在默认端口(6379) 上连接到 localhost。有关配置 Spring 数据 Redis 的更多信息,请参见参考文献 (opens new window)。 |
# WebSession 集成如何工作
与 Servlet API 及其HttpSession
相比, Spring Session 与 Spring WebFlux 及其WebSession
集成要容易得多。 Spring WebFlux 提供了WebSessionStore
API,该 API 提供了用于持久化WebSession
的策略。
本节描述 Spring Session 如何使用WebSession 提供透明的集成。我们提供这些内容,这样你就可以了解幕后发生了什么。这个功能已经集成了,你不需要自己实现这个逻辑。 |
---|
首先,我们创建一个自定义的SpringSessionWebSession
,它将委托给 Spring Session 的Session
。它看起来是这样的:
public class SpringSessionWebSession implements WebSession {
enum State {
NEW, STARTED
}
private final S session;
private AtomicReference<State> state = new AtomicReference<>();
SpringSessionWebSession(S session, State state) {
this.session = session;
this.state.set(state);
}
@Override
public void start() {
this.state.compareAndSet(State.NEW, State.STARTED);
}
@Override
public boolean isStarted() {
State value = this.state.get();
return (State.STARTED.equals(value)
|| (State.NEW.equals(value) && !this.session.getAttributes().isEmpty()));
}
@Override
public Mono<Void> changeSessionId() {
return Mono.defer(() -> {
this.session.changeSessionId();
return save();
});
}
// ... other methods delegate to the original Session
}
接下来,我们创建一个自定义WebSessionStore
,它将委托给ReactiveSessionRepository
,并将Session
封装到自定义WebSession
实现中,如下所示:
public class SpringSessionWebSessionStore<S extends Session> implements WebSessionStore {
private final ReactiveSessionRepository<S> sessions;
public SpringSessionWebSessionStore(ReactiveSessionRepository<S> reactiveSessionRepository) {
this.sessions = reactiveSessionRepository;
}
// ...
}
要被 Spring WebFlux 检测到,此自定义WebSessionStore
需要在ApplicationContext
中注册为 Bean 名为webSessionManager
的 Bean。有关 Spring WebFlux 的更多信息,请参见Spring Framework Reference Documentation (opens new window)。