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. 「Excel实用技巧」拿下这个报表工具,轻松给报表加水印!

    水印报表是什么?怎么给excel表格加水印? 许多时候,为了防止报表被盗.保护信息安全和保护版权,采用水印能够有效实现该需求.水印是为了防止报表被盗.进行真伪鉴别.版权保护等,而在报表中添加半透明的图 ...

  2. Glob 语法及解析

    Glob 语法及解析 目录 1. glob 简介 2. glob 语法 3. .gitignore 4. Python glob 1 glob 简介 glob 是用于匹配符合指定模式的文件集合的一种语 ...

  3. shell-if表达式(-f,-d,-s,-r,-w,-x,-eq,-ne,-ge,-gt,-le,-lt )

    文件表达式 if [ -f file ] 如果文件存在if [ -d - ] 如果目录存在if [ -s file ] 如果文件存在且非空if [ -r file ] 如果文件存在且可读if [ -w ...

  4. C语言每日一题

    66. 加一 /** * Note: The returned array must be malloced, assume caller calls free(). */ /* 从后向前(从个位)开 ...

  5. linux多进/线程编程(1)—— 基础概念(PCB、MMU、进程状态)

    学习大概就是不断迭代.重构的过程,不复习的学习是不负责任的,亦是无用的. 本系列博客主要作为个人记录,主要是贴图和代码,不做详细解释,以后有时间可能会重写:从下一篇开始上代码,代码可以运行是对自己的最 ...

  6. js和C# 编码 解码

    C#中对URL编码的方法... 编码:Server.UrlEncode(string) 解码:Server.UrlDecode(string) HttpUtility.UrlEncode(string ...

  7. 数据库连接池与SQL工具类

    数据库连接池与SQL工具类 1.数据库连接池 依赖包 pymysql dbutils # -*- coding: utf-8 -*- ''' @Time : 2021/11/19 16:45 @Aut ...

  8. react的”Hello World !“

    本文主要简述react的开始使用 1.引入js文件 <!-- 1.核心文件 --> <script crossorigin src="https://unpkg.com/r ...

  9. 您的请求在Web服务器中没有找到对应的站点”这是什么原因?出现这个界面说明域名解析已经正确并生效,这是由于域名没有绑定好,

    宝塔出现 您的请求在Web服务器中没有找到对应的站点"这是什么原因?出现这个界面说明域名解析已经正确并生效,这是由于域名没有绑定好 , 本人经过测试使用如下方法解决.允话空HTTP_REFE ...

  10. Android12 新特性及适配指南

    Android 12(API 31)于2021年10月4日正式发布,正式版源代码也于当日被推送到AOSP Android开源项目.截止到笔者撰写这篇文章时,国内各终端厂商的在售Android设备,已经 ...