【发布时间】:2018-06-11 08:42:36
【问题描述】:
Spring Security 5 提供了一个ReactiveSecurityContextHolder to fetch the SecurityContext from a Reactive context,但是当我想实现AuditorAware 并自动获得试听工作时,它不起作用。 目前我找不到AuditorAware 的Reactive 变体。
@Bean
public AuditorAware<Username> auditor() {
return () -> ReactiveSecurityContextHolder.getContext()
.map(SecurityContext::getAuthentication)
.log()
.filter(a -> a != null && a.isAuthenticated())
.map(Authentication::getPrincipal)
.cast(UserDetails.class)
.map(auth -> new Username(auth.getName()))
.switchIfEmpty(Mono.empty())
.blockOptional();
}
我已经在我的引导Application 类中添加了@EnableMongoAuduting。
在 Mongo 文档类上。我添加了试听相关的注释。
@CreatedDate
private LocalDateTime createdDate;
@CreatedBy
private Username author;
当我添加一个帖子时,createdDate 已填充,但作者为空。
{"id":"5a49ccdb9222971f40a4ada1","title":"my first post","content":"content of my first post","createdDate":"2018-01-01T13:53:31.234","author":null}
完整代码为here,基于Spring Boot 2.0.0.M7。
更新: Spring Boot 2.4.0-M2/Spring Data Common 2.4.0-M2/Spring Data Mongo 3.1.0-M2 包括ReactiveAuditorAware,检查this new sample,注意:使用@EnableReactiveMongoAuditing来激活它。
【问题讨论】:
-
尚无对反应性使用的审核支持。
标签: spring spring-security spring-data-mongodb project-reactor spring-webflux