基本

Spring Data 提供了复杂的支持,可以透明地跟踪谁创建或更改了实体以及更改发生的时间。若要从该功能中受益,必须为实体类配备审计元数据,这些元数据可以使用注释或通过实现接口来定义。 此外,必须通过注释配置或 XML 配置启用审核,以注册所需的基础结构组件。 有关配置示例,请参阅特定于商店的部分。Spring中文文档

不需要仅跟踪创建和修改日期的应用程序确实会使其实体实现 AuditorAwareSpring中文文档

基于注释的审计元数据

我们提供并捕获创建或修改实体的用户,以及捕获更改发生的时间。@CreatedBy@LastModifiedBy@CreatedDate@LastModifiedDateSpring中文文档

经审计的实体
class Customer {

  @CreatedBy
  private User user;

  @CreatedDate
  private Instant createdDate;

  // … further properties omitted
}

如您所见,可以有选择地应用注释,具体取决于要捕获的信息。 这些注释指示在进行更改时进行捕获,可用于 JDK8 日期和时间类型、、 以及旧版 Java 和 类型的属性。longLongDateCalendarSpring中文文档

审核元数据不一定需要位于根级实体中,但可以添加到嵌入式实体中(取决于使用的实际存储),如下面的代码片段所示。Spring中文文档

审核嵌入式实体中的元数据
class Customer {

  private AuditMetadata auditingMetadata;

  // … further properties omitted
}

class AuditMetadata {

  @CreatedBy
  private User user;

  @CreatedDate
  private Instant createdDate;

}

基于接口的审计元数据

如果您不想使用注释来定义审核元数据,则可以让域类实现该接口。它公开了所有审核属性的 setter 方法。AuditableSpring中文文档

AuditorAware

如果您使用 或 ,则审计基础结构需要以某种方式了解当前主体。为此,我们提供了一个 SPI 接口,您必须实现该接口,以告诉基础设施与应用程序交互的当前用户或系统是谁。泛型类型定义属性批注的类型或必须是哪种类型。@CreatedBy@LastModifiedByAuditorAware<T>T@CreatedBy@LastModifiedBySpring中文文档

以下示例显示了使用 Spring Security 对象的接口的实现:AuthenticationSpring中文文档

基于 Spring Security 的实现AuditorAware
class SpringSecurityAuditorAware implements AuditorAware<User> {

  @Override
  public Optional<User> getCurrentAuditor() {

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

该实现访问 Spring Security 提供的对象,并查找您在实现中创建的自定义实例。我们在这里假设您正在通过实现公开域用户,但根据找到的内容,您也可以从任何地方查找它。AuthenticationUserDetailsUserDetailsServiceUserDetailsAuthenticationSpring中文文档

ReactiveAuditorAware

使用响应式基础结构时,您可能希望利用上下文信息来提供或信息。 我们提供了一个SPI接口,您必须实现该接口,以告诉基础设施与应用程序交互的当前用户或系统是谁。泛型类型定义属性批注的类型或必须是哪种类型。@CreatedBy@LastModifiedByReactiveAuditorAware<T>T@CreatedBy@LastModifiedBySpring中文文档

以下示例显示了使用响应式 Spring Security 对象的接口的实现:AuthenticationSpring中文文档

基于 Spring Security 的实现ReactiveAuditorAware
class SpringSecurityAuditorAware implements ReactiveAuditorAware<User> {

  @Override
  public Mono<User> getCurrentAuditor() {

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

该实现访问 Spring Security 提供的对象,并查找您在实现中创建的自定义实例。我们在这里假设您正在通过实现公开域用户,但根据找到的内容,您也可以从任何地方查找它。AuthenticationUserDetailsUserDetailsServiceUserDetailsAuthenticationSpring中文文档

还有一个方便的基类,您可以扩展它以避免手动实现接口方法的需要。这样做会增加域类与 Spring Data 的耦合,这可能是您想要避免的事情。通常,首选基于注释的审计元数据定义方式,因为它侵入性更小且更灵活。AbstractAuditableSpring中文文档

不需要仅跟踪创建和修改日期的应用程序确实会使其实体实现 AuditorAwareSpring中文文档

常规审核配置

Spring Data JPA 附带了一个实体侦听器,可用于触发审计信息的捕获。首先,必须注册 to be used for all entities in your persistence contexts in your file, as shown in the following example:AuditingEntityListenerorm.xmlSpring中文文档

例 1.审核配置orm.xml
<persistence-unit-metadata>
  <persistence-unit-defaults>
    <entity-listeners>
      <entity-listener class="….data.jpa.domain.support.AuditingEntityListener" />
    </entity-listeners>
  </persistence-unit-defaults>
</persistence-unit-metadata>

您还可以使用注释按实体启用,如下所示:AuditingEntityListener@EntityListenersSpring中文文档

@Entity
@EntityListeners(AuditingEntityListener.class)
public class MyEntity {

}
审核功能需要位于类路径上。spring-aspects.jar

在类路径上进行适当修改后,激活审核功能只需将 Spring Data JPA 命名空间元素添加到配置中即可,如下所示:orm.xmlspring-aspects.jarauditingSpring中文文档

例 2.使用 XML 配置激活审核
<jpa:auditing auditor-aware-ref="yourAuditorAwareBean" />

从 Spring Data JPA 1.5 开始,您可以通过使用注释对配置类进行注释来启用审计。您仍必须修改文件并具有类路径。以下示例演示如何使用批注:@EnableJpaAuditingorm.xmlspring-aspects.jar@EnableJpaAuditingSpring中文文档

例 3.使用 Java 配置激活审计
@Configuration
@EnableJpaAuditing
class Config {

  @Bean
  public AuditorAware<AuditableUser> auditorProvider() {
    return new AuditorAwareImpl();
  }
}

如果将 类型的 Bean 公开给 ,则审计基础结构会自动选取该 Bean 并使用它来确定要在域类型上设置的当前用户。如果在 中注册了多个实现,则可以通过显式设置 的属性来选择要使用的实现。AuditorAwareApplicationContextApplicationContextauditorAwareRef@EnableJpaAuditingSpring中文文档

审核功能需要位于类路径上。spring-aspects.jar