|
该版本仍在开发中,尚未被视为稳定。对于最新稳定版本,请使用Spring Data Neo4j 8.0.0! |
审计
基本
Spring Data 提供复杂的支持,透明跟踪谁创建或更改实体及其变更时间。为了享受该功能,你必须为实体类配备审计元数据,这些元数据可以通过注释定义或实现接口来定义。 此外,审计还必须通过注释配置或XML配置来注册所需的基础设施组件。 请参阅商店专属部分的配置示例。
|
仅跟踪创建和修改日期的应用程序不要求其实体实现 |
基于注释的审计元数据
我们提供@CreatedBy和@LastModifiedBy捕捉创建或修改实体的用户,以及@CreatedDate和@LastModifiedDate记录变化发生的时间。
class Customer {
@CreatedBy
private User user;
@CreatedDate
private Instant createdDate;
// … further properties omitted
}
如你所见,注释可以根据你想捕捉的信息选择性应用。
这些注释用于记录何时发生更改,可用于类型为 JDK8 日期和时间的属性,长,长以及遗留的Java日期和日历.
审计元数据不一定必须存在根级实体,但可以添加到嵌入的实体中(具体取决于实际使用的存储),如下面的摘要所示。
class Customer {
private AuditMetadata auditingMetadata;
// … further properties omitted
}
class AuditMetadata {
@CreatedBy
private User user;
@CreatedDate
private Instant createdDate;
}
审计员
如果你用其中一种@CreatedBy或@LastModifiedBy审计基础设施需要以某种方式了解当前的原则。为此,我们提供审计员<T>你必须实现 SPI 接口,告诉基础设施当前与应用交互的用户或系统是谁。通用类型T定义了注释属性的类型@CreatedBy或@LastModifiedBy必须是。
以下示例展示了使用 Spring Security 的接口实现认证对象:
审计员基于Spring Security。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 提供的对象,并查找自定义用户详情你在你的中创造了实例用户详情服务实现。我们假设你通过用户详情但实现,基于认证找到了,你也可以在任何地方查找。
ReactiveAuditorAware
使用响应式基础设施时,你可能需要利用上下文信息来提供@CreatedBy或@LastModifiedBy信息。
我们提供ReactiveAuditorAware<T>你必须实现 SPI 接口,告诉基础设施当前与应用交互的用户或系统是谁。通用类型T定义了注释属性的类型@CreatedBy或@LastModifiedBy必须是。
以下示例展示了使用响应式 Spring Security 的接口实现认证对象:
ReactiveAuditorAware基于Spring Security。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 提供的对象,并查找自定义用户详情你在你的中创造了实例用户详情服务实现。我们假设你通过用户详情但实现,基于认证找到了,你也可以在任何地方查找。