我有一个集成了Spring Security的反应式应用程序,它是由spring initilizer创建的,主要是thre3包( 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
发布于 2020-06-12 20:48:48
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 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。
发布于 2020-06-05 16:17:48
我最终通过实现定制的ServerAuthenticationSuccessHandler解决了这个问题,例如:
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:
@EnableWebFluxSecurity
public class SecurityConfig {
@Bean
SecurityWebFilterChain webFluxSecurityFilterChain(ServerHttpSecurity http) throws Exception {
http.formLogin().authenticationSuccessHandler(new SuccessHandler());
return http.build();
}
}发布于 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://stackoverflow.com/questions/62133366
复制相似问题