审计
基础
Spring Data 提供了完善的支持,可透明地跟踪谁创建或更改了实体以及更改发生的时间。要使用该功能,您必须为实体类配备审计元数据,这些元数据可通过使用注解或实现接口来定义。此外,必须通过注解配置或 XML 配置启用审计,以注册所需的基础设施组件。有关配置示例,请参阅特定存储部分。
仅跟踪创建和修改日期的应用程序无需让其实体实现 AuditorAware。
基于注解的审计元数据
我们提供了 @CreatedBy 和 @LastModifiedBy 来捕获创建或修改实体的用户,并提供了 @CreatedDate 和 @LastModifiedDate 来捕获更改发生的时间。
class Customer {
@CreatedBy
private User user;
@CreatedDate
private Instant createdDate;
// … further properties omitted
}
如您所见,注解可根据您想要捕获的信息选择性地应用。这些用于指示在更改发生时进行捕获的注解,可应用于 JDK8 日期与时间类型、long、Long 以及旧版 Java Date 和 Calendar 类型的属性。
提供时间的实例由 org.springframework.data.auditing.DateTimeProvider 给出。默认情况下使用的是 CurrentDateTimeProvider。您可以在启用审计时通过 dateTimeProviderRef 属性进行更改,也可以在 ApplicationContext 中提供一个专用的 AuditingHandler 或 DateTimeProvider bean。
审计元数据并不一定需要放在根级实体中,也可以添加到内嵌实体上(取决于实际使用的存储),如下方代码片段所示。
class Customer {
private AuditMetadata auditingMetadata;
// … further properties omitted
}
class AuditMetadata {
@CreatedBy
private User user;
@CreatedDate
private Instant createdDate;
}
基于接口的审计元数据
如果您不想使用注解来定义审计元数据,可以让您的领域类实现 Auditable 接口。该接口公开了所有审计属性的 setter 方法。
AuditorAware
如果您使用了 @CreatedBy 或 @LastModifiedBy,审计基础设施需要以某种方式获知当前的主体。为此,我们提供了一个 AuditorAware<T> SPI 接口,您需要实现该接口,以告知基础设施当前与应用程序交互的用户或系统是谁。泛型类型 T 定义了使用 @CreatedBy 或 @LastModifiedBy 注解的属性必须具有的类型。
以下示例展示了使用 Spring Security 的 Authentication 对象实现该接口的方式:
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 提供的 Authentication 对象,并查找您在 UserDetailsService 实现中创建的自定义 UserDetails 实例。这里假设您通过 UserDetails 实现暴露领域用户,但基于找到的 Authentication,您也可以从任何位置查找它。
ReactiveAuditorAware
在使用响应式基础设施时,您可能希望利用上下文信息来提供 @CreatedBy 或 @LastModifiedBy 信息。我们提供了一个 ReactiveAuditorAware<T> SPI 接口,您需要实现该接口,以告知基础设施当前与应用程序交互的用户或系统是谁。泛型类型 T 定义了使用 @CreatedBy 或 @LastModifiedBy 注解的属性必须具有的类型。
以下示例展示了使用响应式 Spring Security 的 Authentication 对象实现该接口的方式:
class SpringSecurityAuditorAware implements ReactiveAuditorAware<User> {
@Override
public Mono<User> getCurrentAuditor() {
return ReactiveSecurityContextHolder.getContext()
.mapNotNull(SecurityContext::getAuthentication)
.filter(Authentication::isAuthenticated)
.mapNotNull(Authentication::getPrincipal)
.map(User.class::cast);
}
}
该实现访问由 Spring Security 提供的 Authentication 对象,并查找您在 UserDetailsService 实现中创建的自定义 UserDetails 实例。这里假设您通过 UserDetails 实现暴露领域用户,但基于找到的 Authentication,您也可以从任何位置查找该用户。
此外还有一个便捷的基类 AbstractAuditable,您可以继承它,以避免手动实现接口方法。这样做会增加领域类与 Spring Data 的耦合,而这可能是您想要避免的。通常,基于注解的方式定义审计元数据更为可取,因为它侵入性更小且更灵活。
通用审计配置
Spring Data JPA 自带一个实体监听器,可用于触发审计信息的捕获。首先,您必须在 orm.xml 文件中注册 AuditingEntityListener,使其在持久化上下文中对所有实体可用,如下例所示:
示例 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>
您也可以通过使用 @EntityListeners 注解,为每个实体单独启用 AuditingEntityListener,如下所示:
@Entity
@EntityListeners(AuditingEntityListener.class)
public class MyEntity {
}
审计功能需要 spring-aspects.jar 位于 classpath 中。
在 orm.xml 适当修改且 spring-aspects.jar 位于 classpath 中的情况下,激活审计功能只需将 Spring Data JPA 的 auditing 命名空间元素添加到您的配置中,如下所示:
示例 2. 使用 XML 配置激活审计功能
<jpa:auditing auditor-aware-ref="yourAuditorAwareBean" />
自 Spring Data JPA 1.5 起,您可以通过在配置类上添加 @EnableJpaAuditing 注解来启用审计。您仍须修改 orm.xml 文件,并将 spring-aspects.jar 置于 classpath 中。以下示例展示了如何使用 @EnableJpaAuditing 注解:
示例 3. 使用 Java 配置激活审计功能
@Configuration
@EnableJpaAuditing
class Config {
@Bean
public AuditorAware<AuditableUser> auditorProvider() {
return new AuditorAwareImpl();
}
}
如果您将一个类型为 AuditorAware 的 bean 暴露给 ApplicationContext,审计基础设施会自动获取它,并用它来确定要在领域类型上设置的当前用户。如果您在 ApplicationContext 中注册了多个实现,可以通过显式设置 @EnableJpaAuditing 的 auditorAwareRef 属性来选择要使用的实现。