SpringBoot项目使用SpringDataJpa提供的审计功能的使用流程

SpringDataJpa提供审计注解:@CreatedBy,@LastModifiedBy,@CreatedDate,@LastModifiedDate

第一步:在SpringBoot启动类上添加@EnableJpaAuditing

public @interface EnableJpaAuditing {

    /**
* Configures the {@link AuditorAware} bean to be used to lookup the current principal.
*
* @return
*/
// 当SpringIOC容器中注册了多个审计的Bean,需要指定Bean的名称
String auditorAwareRef() default ""; /**
* Configures whether the creation and modification dates are set. Defaults to {@literal true}.
*
* @return
*/
boolean setDates() default true; /**
* Configures whether the entity shall be marked as modified on creation. Defaults to {@literal true}.
*
* @return
*/
boolean modifyOnCreate() default true; /**
* Configures a {@link DateTimeProvider} bean name that allows customizing the {@link org.joda.time.DateTime} to be
* used for setting creation and modification dates.
*
* @return
*/
String dateTimeProviderRef() default "";
}

原文:

If you expose a bean of type AuditorAware to the ApplicationContext, the auditing infrastructure automatically picks it up and uses it to determine the current user to be set on domain types. If you have multiple implementations registered in the ApplicationContext, you can select the one to be used by explicitly setting the auditorAwareRef attribute of @EnableJpaAuditing.

翻译:

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

第二步:在实体类上进行添加注解的标注字段

class Customer {

  @CreatedBy
private User user; @CreatedDate
private DateTime createdDate; // … further properties omitted
}

第三步:实现 AuditorAware 接口,泛型T是返回的限定类型,并注册到Spring管理的容器中

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);
}
}

第四步:在实体类上添加 @EntityListeners(AuditingEntityListener.class) 注解

之后,当进行save操作的时候,会自动设置获取通过审计注解获取的相关信息

参考官方文献:https://docs.spring.io/spring-data/jpa/docs/2.1.9.RELEASE/reference/html/#auditing

SpringDataJpa使用审计(Auditing)功能的更多相关文章

  1. 学习Spring-Data-Jpa(十五)---Auditing与@MappedSuperclass

    1.Auditing 一般我们针对一张表的操作需要记录下来,是谁修改的,修改时间是什么,Spring-Data为我们提供了支持. 1.1.在实体类中使用Spring-Data为我们提供的四个注解(也可 ...

  2. abp审计日志功能的关闭

    参考官网介绍:https://aspnetboilerplate.com/Pages/Documents/Audit-Logging

  3. 利用paramiko模块实现堡垒机+审计功能

    paramiko模块是一个远程连接服务器,全真模拟ssh2协议的python模块,借助paramiko源码包中的demos目录下:demo.py和interactive.py两个模块实现简单的堡垒机+ ...

  4. [置顶] 使用struts拦截器+注解实现网络安全要求中的日志审计功能

    J2EE项目中出于安全的角度考虑,用户行为审计日志功能必不可少,通过本demo可以实现如下功能: 1.项目中记录审计日志的方法. 2.struts拦截器的基本配置和使用方法. 3.struts拦截器中 ...

  5. percona mysql5.7关闭审计功能方法

    数据库的审计日志占用大量空间,当时是为了测试审计功能开启的,现在需要关闭 # /data/mysql_data]# du -sh * 124G audit.log # 查询审计相关参数 mysql&g ...

  6. 通过init-connect + binlog 实现MySQL审计功能

    背景: 假设这么一个情况,你是某公司mysql-DBA,某日突然公司数据库中的所有被人为删了. 尽管有数据备份,但是因服务停止而造成的损失上千万,现在公司需要查出那个做删除操作的人. 但是拥有数据库操 ...

  7. ABP源码分析十九:Auditing

    审计跟踪(也叫审计日志)是与安全相关的按照时间顺序的记录,它们提供了活动序列的文档证据,这些活动序列可以在任何时间影响一个特定的操作. AuditInfo:定义如下图中需要被Audit的信息. Aud ...

  8. Abp + MongoDb 改造默认的审计日志存储位置

    一.背景 在实际项目的开发当中,使用 Abp Zero 自带的审计日志功能写入效率比较低.其次审计日志数据量中后期十分庞大,不适合与业务数据存放在一起.所以我们可以重新实现 Abp 的 IAuditi ...

  9. Oracle11g温习-第十九章:审计(audit)

    2013年4月27日 星期六 10:52 1.审计的功能:监控用户在database 的 action (操作) 2.审计分类 1) session :在同一个session,相同的语句只产生一个审计 ...

随机推荐

  1. [Java]Java中的自动包装

    来源:https://www.cnblogs.com/cheapcrook/archive/2012/04/25/2470478.html 自动拆装箱(AutoBoxing) 是JDK1.5中新增加的 ...

  2. 五分钟,手撸一个Spring容器!

    大家好,我是老三,Spring是我们最常用的开源框架,经过多年发展,Spring已经发展成枝繁叶茂的大树,让我们难以窥其全貌. 这节,我们回归Spring的本质,五分钟手撸一个Spring容器,揭开S ...

  3. 执行上下文与同步上下文 | ExecutionContext 和 SynchronizationContext

    原文连接:执行上下文与同步上下文 - .NET 并行编程 (microsoft.com) 执行上下文与同步上下文 斯蒂芬 6月15日, 2012 最近,我被问了几次关于 ExecutionContex ...

  4. C# 重载运算符--不合理设计,只支持静态

    什么叫做重载运算符 比如:int x=1; int y=2; int total=x+y; 我们比较喜欢看上面这种写法,而不是这种, int x=1; int y=1; int total=int.a ...

  5. mongodb之shard分片

    总的 1:在3台独立服务器上,分别运行 27017,27018,27019实例, 互为副本集,形成3套repl set 2: 在3台服务器上,各配置config server, 运行27020端口上 ...

  6. 基于Netty的一个WeoSocket通信服务器与客户端代码(非JS代码)

    基于Netty的一个WeoSocket通信服务器与客户端代码(非JS代码) 咳咳,在这里呢,小轩就不多说什么是WebSocket的,还有呢,小轩为什么不给出JS-Client代码?网上太多代码可以用了 ...

  7. Python:读取Excel表格时出现的u'\u51c6’ 无法正确显示汉字

    读取Excel后,想显示其中一行的元素,结果读出来是这样[u'\u51c6\u8003\u8bc1\u53f7', u'\u8003\u751f\u59d3\u540d'],始终不显示正常的汉字 依照 ...

  8. c/c++(c++和网络编程)日常积累(三)

    asio::transfer_all() 有空研究一下这个字段 malloc和new的区别 https://blog.csdn.net/weixin_39411321/article/details/ ...

  9. Ajax 实战(一)

    目录 Ajax 实战(一) 简介 入门案例 基于Ajax进行登录验证 HTTP请求编码格式和报文 Content-Type=application/x-www-form-urlencoded Cont ...

  10. LeetCode-054-螺旋矩阵

    螺旋矩阵 题目描述:给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素. 示例说明请见LeetCode官网. 来源:力扣(LeetCode) 链接:http ...