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
AuditorAwareto 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 theauditorAwareRefattribute 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,相同的语句只产生一个审计 ...
随机推荐
- 传输层隧道技术之lcx内网端口转发
传输层技术包括TCP隧道.UDP隧道和常规端口转发等.在渗透测试中,如果内网防火墙阻止了指定端口的访问,在获得目标机器的权限后,可以使用IPTABLES打开指定端口.如果内网中存在一系列防御系统,TC ...
- Vue UI API简单笔记
VUE UI 目录 VUE UI 一 移动端常用UI组件库 二 PC端常用UI组件库 三 ElementUI组件按需引入 一 移动端常用UI组件库 Vant http://vant-contrib.g ...
- 国内商业智能软件的天下来了,选择BI工具我们应该看这3点!
数据变革时代,经常有"外行人"问到,何谓商业智能BI? BI可以说是通过软件或者服务,将网络时代中海量的数据转化为行动上的洞察力,从而影响企业的战略和战术决策. 当海量.高增长率 ...
- while read line [linux] shell 学习
转至:https://blog.csdn.net/qq_22083251/article/details/80484176 循环中的重定向 或许你应该在其他脚本中见过下面的这种写法: while re ...
- 2019CCPC Final K. Russian Dolls on the Christmas Tree
题目大意 一棵 \(n(1\leq n\leq 2\times 10^5)\) 个节点以 \(1\) 为根的树,分别求以 \(1\sim n\) 为根的子树中有多少个节点编号连续的段. \(T(1\l ...
- Qt:QString
0.说明 区别于QByteArray,QString串是Unicode串,每个元素都是QChar 16-bit UTF-16编码(Unicode) :而QByteArray是8-bit串. 0.1.初 ...
- WPS:将题注与章节连接起来
右键图片添加题注,在题注设置中勾选题注编号中的包含章节编号
- Pandas:loc iloc ix用法
参考:Pandas中关于 loc \ iloc \ ix 用法的理解 相同点 使用形式都是 df.xxx[ para1 , para2 ] #xxx表示loc iloc ix#df表示一个DataFr ...
- LeetCode-005-最长回文子串
最长回文子串 题目描述:给你一个字符串 s,找到 s 中最长的回文子串. 示例说明请见LeetCode官网. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/pr ...
- 浏览器中 Http缓存
分类: web缓存主要有:数据库缓存.服务器缓存(代理服务器缓存.CDN缓存),浏览器缓存. 数据库缓存 当web应用的关系比较复杂,数据库中的表很多的时候,如果频繁进行数据库查询,很容易导致数据库不 ...