Vol*_*kyi 10 java spring jpa spring-data spring-data-jpa
我@CreatedDate在实体属性上使用,我看到它在db中插入日期.我不明白@CreatedBySpring Data JPA 中注释的目的是什么.
在参考文档中,我读到:
我们提供
@CreatedBy,@LastModifiedBy捕捉谁创建或修改该实体的用户
但是如何创建和使用这样的用户呢?
for TL;DR
your Entity
@CreatedBy
private UUID modifyBy;
Run Code Online (Sandbox Code Playgroud)
and your SecurityAuditorAware
@Component
public class SecurityAuditorAware implements AuditorAware<UUID> {
@Override
public Optional<UUID> getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || !authentication.isAuthenticated()) {
return Optional.empty();
}
return Optional.of(((MyCustomUser) authentication.getPrincipal()).getId());
}
}
Run Code Online (Sandbox Code Playgroud)
note: you need use the same data type, I use UUID as my custom user id.
| 归档时间: |
|
| 查看次数: |
11941 次 |
| 最近记录: |