首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring reactive的spring安全会话超时

Spring reactive的spring安全会话超时
EN

Stack Overflow用户
提问于 2020-06-01 21:30:10
回答 3查看 2.9K关注 0票数 3

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

我尝试通过在application.properties中执行以下配置来配置会话超时

代码语言:javascript
复制
spring.session.timeout=1m

在使用mvn spring-boot:run启动应用程序后,http://localhost:8080可以访问它,它会要求我登录(默认的安全设置)。我可以使用在控制台上生成的用户名user和密码登录。

根据我的配置,我预计在1分钟的空闲时间后,当我再次刷新页面http://localhost:8080时,它会要求我重新登录。但实际上它并没有,直到30分钟后

所以我怀疑上面的配置不起作用

我是否使用了错误的配置?

可在此处找到复制复制品:https://github.com/ZhuBicen/ReactiveSpringSecurity.git

EN

回答 3

Stack Overflow用户

发布于 2020-06-12 20:48:48

Spring应该允许为上面的情况自动配置反应式堆栈,就像它对servlet所做的那样。

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

首先,添加spring session依赖项

代码语言:javascript
复制
    <dependency>
      <groupId>org.springframework.session</groupId>
      <artifactId>spring-session-core</artifactId>
    </dependency>

其次,手动创建ReactiveSessionRepository bean。(注意:如果您使用Redis而不是内存等,则可以自动为您配置此设置。)

代码语言:javascript
复制
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

票数 4
EN

Stack Overflow用户

发布于 2020-06-05 16:17:48

我最终通过实现定制的ServerAuthenticationSuccessHandler解决了这个问题,例如:

代码语言:javascript
复制
class SuccessHandler extends RedirectServerAuthenticationSuccessHandler {

    @Override
    public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, Authentication authentication) {
        // set to -1 means the session will never expired
        // webFilterExchange.getExchange().getSession().subscribe(session->session.setMaxIdleTime(Duration.ofSeconds(-1)));
        webFilterExchange.getExchange().getSession().subscribe(session->session.setMaxIdleTime(Duration.ofMinutes(60)));
        return super.onAuthenticationSuccess(webFilterExchange, authentication);
    }
}

可以通过类似的方式设置SuccessHandler:

代码语言:javascript
复制
@EnableWebFluxSecurity
public class SecurityConfig {
    @Bean
    SecurityWebFilterChain webFluxSecurityFilterChain(ServerHttpSecurity http) throws Exception {
        http.formLogin().authenticationSuccessHandler(new SuccessHandler());
        return http.build();
    }
}
票数 2
EN

Stack Overflow用户

发布于 2020-06-02 03:51:56

您配置的参数与您使用web浏览器进行的cookie会话无关。

该参数配置Spring Session,这是一种通过头部提供会话来处理服务、rest api等之间的会话的方法。

https://spring.io/projects/spring-session

会话cookie在历史上绑定到线程本地中的线程,这在反应式应用程序中不起作用。因此,为了存储会话,您需要能够将会话存储在其他地方。Redis就是一个可以存储websessions的例子。

这是一个使用webflux,redis和spring session来管理websession的教程。

https://www.baeldung.com/spring-session-reactive

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62133366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档