我有一个集成了 Spring Security 的反应式应用程序,它是由 springinitializr 创建的,主要包含 3 个包(spring boot、spring security 和 webflux)。
我试图通过以下配置来配置会话超时application.properties
:
spring.session.timeout=1m
启动应用程序后mvn spring-boot:run
, 可以通过以下方式访问http://localhost:8080
它要求我登录(默认安全设置)。我可以使用用户名user
以及控制台生成的登录密码。
根据我的配置,我预计在 1 分钟空闲时间后,当我刷新页面时http://localhost:8080
再次,它可以要求我重新登录。但事实上并没有,直到30分钟后
所以我怀疑上面的配置不起作用
我使用了错误的配置吗?
可以在这里找到重现存储库:https://github.com/ZhuBicen/ReactiveSpringSecurity.git
Spring 可能应该允许针对上述情况对反应式堆栈进行自动配置,就像对 servlet 所做的那样。
然而,“会话”是状态,除非有一些持久存储支持,否则该状态不会扩展。您可以将 Spring Session 抽象与内存中的ReactiveSessionRepository
即使您(还)没有像 Redis 之类的后备存储。当您确实获得适当支持的后备存储并添加相应的依赖项时,您可以删除内存中的内容ReactiveSessionRepository
因为 Spring Boot 会自动配置你的ReactiveSessionRepository
为你。
首先添加spring session依赖
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
其次,手动创建您的ReactiveSessionRepository
豆。 (注意:如果您使用 Redis 而不是内存等,则可以为您自动配置)
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.session.SessionProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.ReactiveMapSessionRepository;
import org.springframework.session.ReactiveSessionRepository;
import org.springframework.session.config.annotation.web.server.EnableSpringWebSession;
import java.util.concurrent.ConcurrentHashMap;
/**
* This ReactiveSessionRepository isn't auto-configured so we need to create it and manually set the timeout on it.
* Later, ReactiveRedisSessionRepository will be auto-configured so we can delete this
*/
// https://www.baeldung.com/spring-session-reactive#in-memory-configuration
@Configuration
@EnableSpringWebSession
@RequiredArgsConstructor // if lombok
@Slf4j // if lombok
public class SessionConfig {
private final SessionProperties sessionProperties;
@Bean
public ReactiveSessionRepository reactiveSessionRepository() {
ReactiveMapSessionRepository sessionRepository = new ReactiveMapSessionRepository(new ConcurrentHashMap<>());
int defaultMaxInactiveInterval = (int) sessionProperties.getTimeout().toSeconds();
sessionRepository.setDefaultMaxInactiveInterval(defaultMaxInactiveInterval);
log.info("Set in-memory session defaultMaxInactiveInterval to {} seconds.", defaultMaxInactiveInterval);
return sessionRepository;
}
}
三、设置属性spring.session.timeout=3600
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)