SpringDataJpa使用审计(Auditing)功能
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 theApplicationContext
, 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 theApplicationContext
, you can select the one to be used by explicitly setting theauditorAwareRef
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)功能的更多相关文章
- 学习Spring-Data-Jpa(十五)---Auditing与@MappedSuperclass
1.Auditing 一般我们针对一张表的操作需要记录下来,是谁修改的,修改时间是什么,Spring-Data为我们提供了支持. 1.1.在实体类中使用Spring-Data为我们提供的四个注解(也可 ...
- abp审计日志功能的关闭
参考官网介绍:https://aspnetboilerplate.com/Pages/Documents/Audit-Logging
- 利用paramiko模块实现堡垒机+审计功能
paramiko模块是一个远程连接服务器,全真模拟ssh2协议的python模块,借助paramiko源码包中的demos目录下:demo.py和interactive.py两个模块实现简单的堡垒机+ ...
- [置顶] 使用struts拦截器+注解实现网络安全要求中的日志审计功能
J2EE项目中出于安全的角度考虑,用户行为审计日志功能必不可少,通过本demo可以实现如下功能: 1.项目中记录审计日志的方法. 2.struts拦截器的基本配置和使用方法. 3.struts拦截器中 ...
- percona mysql5.7关闭审计功能方法
数据库的审计日志占用大量空间,当时是为了测试审计功能开启的,现在需要关闭 # /data/mysql_data]# du -sh * 124G audit.log # 查询审计相关参数 mysql&g ...
- 通过init-connect + binlog 实现MySQL审计功能
背景: 假设这么一个情况,你是某公司mysql-DBA,某日突然公司数据库中的所有被人为删了. 尽管有数据备份,但是因服务停止而造成的损失上千万,现在公司需要查出那个做删除操作的人. 但是拥有数据库操 ...
- ABP源码分析十九:Auditing
审计跟踪(也叫审计日志)是与安全相关的按照时间顺序的记录,它们提供了活动序列的文档证据,这些活动序列可以在任何时间影响一个特定的操作. AuditInfo:定义如下图中需要被Audit的信息. Aud ...
- Abp + MongoDb 改造默认的审计日志存储位置
一.背景 在实际项目的开发当中,使用 Abp Zero 自带的审计日志功能写入效率比较低.其次审计日志数据量中后期十分庞大,不适合与业务数据存放在一起.所以我们可以重新实现 Abp 的 IAuditi ...
- Oracle11g温习-第十九章:审计(audit)
2013年4月27日 星期六 10:52 1.审计的功能:监控用户在database 的 action (操作) 2.审计分类 1) session :在同一个session,相同的语句只产生一个审计 ...
随机推荐
- linux中()、[]、{}、(())、[[]]等各种括号的使用
转至:https://www.jianshu.com/p/b88c7e07aaa9 linux中().[].{}.(()).[[]]等各种括号的使用 1.小括号.圆括号() 1.1 单小括号() 命令 ...
- Chrome:查看用户代理User-Agent
用户代理(User-Agent)是浏览器客户端与服务器交互时的重要信息之一,用于帮助服务器识别请求用户的浏览器类别,以便于网站发送相应的网页数据. 用户代理数据包括:操作系统标识.加密等级标识和浏览器 ...
- linux多进/线程编程(4)——进程间通信之pipe和fifo
前言: Linux环境下,进程地址空间相互独立,每个进程各自有不同的用户地址空间.任何一个进程的全局变量在另一个进程中都看不到,所以进程和进程之间不能相互访问,要交换数据必须通过内核,在内核中开辟一块 ...
- C# Md5Hash
/// <summary> /// MD5 32位加密(大写) /// </summary> /// <param name="str">< ...
- LeetCode-079-单词搜索
单词搜索 题目描述:给定一个 m x n 二维字符网格 board 和一个字符串单词 word .如果 word 存在于网格中,返回 true :否则,返回 false . 单词必须按照字母顺序,通过 ...
- SQL Server--设置用IP地址登录
问题概述:新安装的SQL Server数据库无法用IP地址登录. 是因为 SQL Server "服务器网络实用工具"中禁用了"命名管道"所致! 在sqlse ...
- Spring Cloud Gateway actuator组建对外暴露RCE问题漏洞分析
Spring Cloud gateway是什么? Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关.网关作为流量的,在微服务系统中有着非常作 ...
- (六)React Ant Design Pro + .Net5 WebApi:后端环境搭建-EF Core
一. 简介 EFCore 是轻量化.可扩展.开源和跨平台版的常用数据访问技术,走你(官方文档) 二. 使用 1.安装数据库驱动包.PMC 工具包 不同的数据库有不同的包,参考,我用 PostgreSQ ...
- Laravel 8 图片上传七牛云
1.利用 composer 下载依赖包 composer require itbdw/laravel-storage-qiniu 2.打开 config 文件夹下的 app.php 文件,在 prov ...
- cookie、session和Storage
概念: cookie:HTTP响应头的一部分,通过name=value的形式存储,主要用于保存登录信息.在设置的cookie过期时间之前一直有效,即使窗口或浏览器关闭. 存放数据大小为4K左右 .有个 ...