gpt4 book ai didi

java - 将 spring-security 与 spring-webflux 结合使用时禁用 WebSession 创建

转载 作者:行者123 更新时间:2023-12-02 09:55:12 27 4
gpt4 key购买 nike

我正在使用 Rest api 运行无状态 spring-boot 应用程序,并且想要禁用 WebSession 的创建,如 https://www.baeldung.com/spring-security-session 中所述。

我创建了自己的 WebSessionManager,它不存储 session 。

   @Bean
public WebSessionManager webSessionManager() {
return new WebSessionManager() {
@Override
@NonNull
public Mono<WebSession> getSession(@NonNull final ServerWebExchange exchange) {
return Mono.just(new WebSession() {

@Override
@NonNull
public String getId() {
return "";
}

@Override
@NonNull
public Map<String, Object> getAttributes() {
return new HashMap<>();
}

@Override
public void start() {
}

@Override
public boolean isStarted() {
return true;
}

@Override
@NonNull
public Mono<Void> changeSessionId() {
return Mono.empty();
}

@Override
@NonNull
public Mono<Void> invalidate() {
return Mono.empty();
}

@Override
@NonNull
public Mono<Void> save() {
return Mono.empty();
}

@Override
public boolean isExpired() {
return false;
}

@Override
@NonNull
public Instant getCreationTime() {
return Instant.now();
}

@Override
@NonNull
public Instant getLastAccessTime() {
return Instant.now();
}

@Override
public void setMaxIdleTime(@NonNull final Duration maxIdleTime) {
}

@Override
@NonNull
public Duration getMaxIdleTime() {
return Duration.ofMinutes(1);
}
});
}
};
}

它有效,但我想知道是否有更好的方法来不创建 session 。

最佳答案

Issue #6552: Session Creation Policy with Webflux Security Spring 团队将修复该问题。

The problem is that the request cache is being invoked for every request to see if there is a value saved to replay and thus the WebSession is being looked up for every request. Since the WebSession is being looked up with an invalid session id, Spring WebFlux invalidates the SESSION cookie. ~ rwinch

DarrenJiang1990建议的解决方案是:

.and().securityContextRepository(NoOpServerSecurityContextRepository.getInstance())

The security context in a WebFlux application is stored in a ServerSecurityContextRepository. Its WebSessionServerSecurityContextRepository implementation, which is used by default, stores the context in session. Configuring a NoOpServerSecurityContextRepository instead would make our application stateless

您可以在 Issue #7157 ServerRequestCacheWebFilter causes WebSession to be read every request 中跟踪修补进度.

关于java - 将 spring-security 与 spring-webflux 结合使用时禁用 WebSession 创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56056404/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com