Spring Security 5 предоставляет ReactiveSecurityContextHolder
для извлечения SecurityContext
из контекста Reactive, но когда я хочу реализовать AuditorAware
и получить работу для прослушивания автоматически, но не работает. В настоящее время я не могу найти вариант Reactive
для 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();
}
Я добавил @EnableMongoAuduting
в класс загрузки Application
.
В классе документов 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}
Полные коды находятся здесь на основе Spring Boot 2.0. 0.М7.
Обновление: Spring Boot 2.4.0-M2/Spring Data Common 2.4.0-M2/Spring Data Mongo 3.1.0-M2 включает в себя ReactiveAuditorAware
, проверьте этот новый пример, Примечание: используйте @EnableReactiveMongoAuditing
для активации Это.