【问题标题】:Auditing (@CreatedDate) does not work with Spring Boot Cassandra审计(@CreatedDate)不适用于 Spring Boot Cassandra
【发布时间】:2020-02-20 00:08:45
【问题描述】:

我有一个简单的实体,我想让“createdDate”自动分配。

@CreatedDate
private LocalDateTime createdDate;

正如documentation 所述,我还在我的 CassandraConfig 中添加了注释“@EnableCassandraAuditing”:

@Configuration
@EnableCassandraRepositories
@EnableCassandraAuditing
public class CassandraConfig extends AbstractCassandraConfiguration { 

但它仍然不起作用.. 实体是使用 createdDate=null 创建的。 任何帮助表示赞赏。

【问题讨论】:

  • 缺少配置您的Auditable User,您需要创建自己的实现,因为这不起作用。

标签: spring spring-boot cassandra spring-data


【解决方案1】:

根据文档说:

我们提供@CreatedBy@LastModifiedBy 来捕获创建的用户 或修改实体以及@CreatedDate@LastModifiedDate 捕捉发生这种情况的时间点。

我认为最后一句话的意思是capture the point in time this happened by some user.,因为我很久以前有同样的issue

那么如果你想@CreatedDate注解工作那么你需要给一个当前的审计员。应该通过以下方式或只是第二个示例来创建您的自定义Auditor Aware

第一个例子:

class SpringSecurityAuditorAware implements AuditorAware<User> {

    public Optional<User> getCurrentAuditor() {

        return Optional.ofNullable(SecurityContextHolder.getContext())
                 .map(SecurityContext::getAuthentication)
                 .filter(Authentication::isAuthenticated)
                 .map(Authentication::getPrincipal)
                 .map(User.class::cast);
    }
}

第二个例子:

class SpringSecurityAuditorAware implements AuditorAware<String> {

    public Optional<String> getCurrentAuditor() {
         return Optional.of("CurrentUser");
    }
}

注意

我找到了一个类似的答案,您可以查看@CreatedDate does not work answer

【讨论】:

  • "如果您使用 @CreatedBy 或 LastModifiedBy,审计基础架构需要以某种方式了解当前主体。" 您只需要 CreatedBy 和 LastModifiedBy 注释。 . 为什么“createdDate”需要用户实体?
  • 请删除upvoted @akcasoy 因为我遇到了同样的问题,当你想@CreatedDate 注释有效时,你需要给当前的审计员。根据文档说:We provide @CreatedBy, @LastModifiedBy to capture the user who created or modified the entity as well as @CreatedDate and @LastModifiedDate to capture the point in time this happened.我认为最后一个意味着捕获某些用户发生的时间点。你可以检查这个答案stackoverflow.com/a/20508028/10426557
  • 对不起.. 我真的尝试过,但它不起作用。这也没有意义..如果我根本没有安全感怎么办?如果我只想从没有任何用户的公共端点保存一些实体怎么办?正如我之前所说,文档清楚地指出“如果您使用 @CreatedBy 或 LastModifiedBy,审计基础架构需要以某种方式了解当前主体
  • 是的,我同意这一点,但我认为这更多是与@CreatedDate 应该如何工作有关的一个问题,它不起作用很奇怪。如果您提供一个小代码示例,这样我也可以重现并帮助您:)@akcasoy
  • 有人尝试成功了吗?你有例子吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
  • 2020-02-24
  • 2016-09-08
  • 2017-03-15
  • 2016-09-16
  • 2018-04-02
相关资源
最近更新 更多