【发布时间】:2018-12-21 06:13:56
【问题描述】:
我一直在 Spring Security + Webflux 中使用 ReactiveAuthenticationManager。它被定制为返回UsernamePasswordAuthenticationToken 的实例,据我所知,这是我在调用ReactiveSecurityContextHolder.getContext().map(ctx -> ctx.getAuthentication()).block() 时应该收到的。据我所知,我无法通过两者访问身份验证上下文:
SecurityContextHolder.getContext().getAuthentication();
或
ReactiveSecurityContextHolder.getContext().map(ctx -> ctx.getAuthentication()).block()
并且尝试从控制器或组件访问它们会解析为null。我怀疑我是否真的在我的自定义管理器中返回了 Authentication 实例,看起来我是:
@Override
public Mono<Authentication> authenticate(final Authentication authentication) {
if (authentication instanceof PreAuthentication) {
return Mono.just(authentication)
.publishOn(Schedulers.parallel())
.switchIfEmpty(Mono.defer(this::throwCredentialError))
.cast(PreAuthentication.class)
.flatMap(this::authenticatePayload)
.publishOn(Schedulers.parallel())
.onErrorResume(e -> throwCredentialError())
.map(userDetails -> new AuthenticationToken(userDetails, userDetails.getAuthorities()));
}
return Mono.empty();
}
其中PreAuthentication 是AbstractAuthenticationToken 的一个实例,AuthenticationToken 扩展UsernamePasswordAuthenticationToken
有趣的是,虽然ReactiveSecurityContextHolder.getContext().map(ctx -> ctx.getAuthentication()).block() 在控制器中不起作用,但我可以将带有@AuthenticationPrincipal 注释的身份验证主体作为方法参数成功注入控制器中。
这似乎是一个配置问题,但我不知道在哪里。有人知道为什么我不能返回身份验证吗?
【问题讨论】:
标签: java spring spring-security spring-webflux project-reactor