spring-boot - Spring react 的 Spring 安全 session 超时

标签 spring-boot spring-security spring-webflux

我有一个集成了 Spring Security 的响应式应用程序,它是由 spring initilizer 创建的,主要包含三个包(spring boot、spring security 和 webflux)。

我试图按照 application.properties 中的配置来配置 session 超时:

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 一样为您的上述情况为响应式(Reactive)堆栈进行自动配置。

但是,“ session ”是状态,除非有一些持久性存储支持它,否则该状态不会扩展。您可以将 Spring Session 抽象与内存中的 ReactiveSessionRepository 一起使用,即使您(还)没有像 Redis 之类的后备存储。当您获得适当的支持后备存储并添加相应的依赖项时,您可以删除内存中的 ReactiveSessionRepository,因为 spring boot 会为您自动配置您的 ReactiveSessionRepository

首先,添加spring session依赖

    <dependency>
      <groupId>org.springframework.session</groupId>
      <artifactId>spring-session-core</artifactId>
    </dependency>

其次,手动创建您的 ReactiveSessionRepository bean。 (注意:如果您使用 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

关于spring-boot - Spring react 的 Spring 安全 session 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62133366/

相关文章:

java - org.thymeleaf.exceptions.TemplateProcessingException : Exception evaluating SpringEL expression: "workout.exerciseName"

grails - 查找当前登录用户的用户角色

使用 spring webMVC 和 spring security 进行 ajax 登录

java - Spring Security 默认登录表单无法加载 CSS 文件(ERR_CONNECTION_TIMED_OUT)

java - JSON 解析错误 : Can not construct instance of io. starter.topic.Topic

spring-boot - Spring Boot 负载均衡

spring-mvc - 允许通过GET请求访问默认 Controller 映射

java - Spring Reactive 使用 FLUX 或 MONO 使用 POST

java - 使用 Spring Security 启用 h2 控制台

java - 使用新参数重复 WebClient 获取