问题描述
Spring Security 5 提供了一个 ReactiveSecurityContextHolder
来从 Reactive 中获取 SecurityContext
上下文,但是当我想实现 AuditorAware
并自动获得试听工作时,但它不起作用.目前我找不到 AuditorAware
的 Reactive
变体.
Spring Security 5 provides a ReactiveSecurityContextHolder
to fetch the SecurityContext
from a Reactive context, but when I want to implement AuditorAware
and get audition work automatically, but it does not work. Currently I can not find a Reactive
variant for AuditorAware
.
@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
.
I have added @EnableMongoAuduting
on my boot Application
class.
关于 Mongo 文档类.我添加了与试听相关的注释.
On the Mongo document class. I added audition related annotations.
@CreatedDate
private LocalDateTime createdDate;
@CreatedBy
private Username author;
当我添加帖子时,createdDate
已填满,但作者为空.
When I added a post, the createdDate
is filled, but author is null.
{"id":"5a49ccdb9222971f40a4ada1","title":"my first post","content":"content of my first post","createdDate":"2018-01-01T13:53:31.234","author":null}
完整代码在这里,基于Spring启动 2.0.0.M7.
The complete codes is here, based on 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
,检查这个新样本,注意:使用@EnableReactiveMongoAuditing
来激活它.
Update: Spring Boot 2.4.0-M2/Spring Data Common 2.4.0-M2/Spring Data Mongo 3.1.0-M2 includes a ReactiveAuditorAware
, Check this new sample, Note: use @EnableReactiveMongoAuditing
to activiate it.
推荐答案
已弃用:请参阅原帖中的更新解决方案
在提供官方 reactive AuditAware 之前,有一种替代方法可以通过 Spring Data Mongo 特定的 ReactiveBeforeConvertCallback
来实现这些.
Deprecated: see the update solution in the original post
Before the official reactive AuditAware is provided, there is an alternative to implement these via Spring Data Mongo specific ReactiveBeforeConvertCallback
.
- 不要使用
@EnableMongoAuditing
- 实现你自己的
ReactiveBeforeConvertCallback
,这里我使用PersistentEntity
接口来处理那些需要审计的实体.
- Do not use
@EnableMongoAuditing
- Implement your own
ReactiveBeforeConvertCallback
, here I use aPersistentEntity
interface for those entities that need to be audited.
public class PersistentEntityCallback implements ReactiveBeforeConvertCallback<PersistentEntity> {
@Override
public Publisher<PersistentEntity> onBeforeConvert(PersistentEntity entity, String collection) {
var user = ReactiveSecurityContextHolder.getContext()
.map(SecurityContext::getAuthentication)
.filter(it -> it != null && it.isAuthenticated())
.map(Authentication::getPrincipal)
.cast(UserDetails.class)
.map(userDetails -> new Username(userDetails.getUsername()))
.switchIfEmpty(Mono.empty());
var currentTime = LocalDateTime.now();
if (entity.getId() == null) {
entity.setCreatedDate(currentTime);
}
entity.setLastModifiedDate(currentTime);
return user
.map(u -> {
if (entity.getId() == null) {
entity.setCreatedBy(u);
}
entity.setLastModifiedBy(u);
return entity;
}
)
.defaultIfEmpty(entity);
}
}
检查完整代码这里.
这篇关于如何使 AuditorAware 与 Spring Data Mongo Reactive 一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!