Spring Data提供支持审计功能:即由谁在什么时候创建或修改实体。Spring Data提供了在实体类的属性上增加@CreatedBy,@LastModifiedBy,@CreatedDate,@LastModifiedDate注解,并配置相应的配置项,即可实现审计功能,有系统自动记录createdBy
CreatedDate
lastModifiedBy
lastModifiedDate
四个属性的值,下面为具体的配置项。
示例
创建一个实体类
package com.hfcsbc.infrastructureservice.domain;
import com.hfcsbc.repository.support.domain.AbstractAuditingEntity;
import lombok.Data;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.util.Date;
/**
* Create by pengchao on 2018/3/7
*/
@Entity
@Data
@EntityListeners({AuditingEntityListener.class})
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
private Integer age;
@CreatedBy
@Column(
name = "created_by",
nullable = false,
length = 50,
updatable = false
)
private String createdBy;
@CreatedDate
@Column(
name = "created_date",
nullable = false,
updatable = false
)
private Date createdDate = new Date();
@LastModifiedBy
@Column(
name = "last_modified_by",
length = 50
)
private String lastModifiedBy;
@LastModifiedDate
@Column(
name = "last_modified_date"
)
private Date lastModifiedDate = new Date();
}
创建相应的Repository
package com.hfcsbc.repository;
import com.hfcsbc.domain.Person;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Create by pengchao on 2018/3/7
*/
public interface PersonRepository extends JpaRepository<Person, Long> {
}
配置获取用户信息的bean
package com.hfcsbc.infrastructureservice.config;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import java.util.Optional;
/**
* Create by pengchao on 2018/3/7
*/
@Component("auditorAware")
public class AuditorAwareImpl implements AuditorAware<String> {
@Override
public Optional<String> getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return Optional.of(authentication.getPrincipal().toString());
}
}
在Spring Boot入口类开启审计功能
package com.hfcsbc.infrastructureservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableJpaAuditing(auditorAwareRef = "auditorAware")
@EnableAsync
public class PersonApplication {
public static void main(String[] args) {
SpringApplication.run(PersonApplication.class, args);
}
}
即完成配置,在使用repository
保存对象时,createdBy
CreatedDate
lastModifiedBy
lastModifiedDate
有审计功能自动插入
注:在异步方法中如何获取用户信息
由于在异步方法中使用repository保存对象,获取不到用户用户信息,需增加如下配置项,即可在Authentication获取用户的信息
package com.hfcsbc.config;
import org.springframework.beans.factory.config.MethodInvokingFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.context.SecurityContextHolder;
/**
* Create by pengchao on 2018/3/7
*/
@Configuration
public class AuditorAwareConfig {
@Bean
public MethodInvokingFactoryBean methodInvokingFactoryBean() {
MethodInvokingFactoryBean methodInvokingFactoryBean = new MethodInvokingFactoryBean();
methodInvokingFactoryBean.setTargetClass(SecurityContextHolder.class);
methodInvokingFactoryBean.setTargetMethod("setStrategyName");
methodInvokingFactoryBean.setArguments(new String[]{SecurityContextHolder.MODE_INHERITABLETHREADLOCAL});
return methodInvokingFactoryBean;
}
}
SecurityContextHolder的主要功能是将当前执行的进程和SecurityContext关联起来。SecurityContextHolder.MODE_INHERITABLETHREADLOCAL
:用于线程有父子关系的情景中,子线程继承父线程的SecurityContextHolder;SecurityContextHolder.MODE_INHERITABLETHREADLOCAL
:全局共用SecurityContextHolder。
源码地址:http://www.wisely.top/2018/03/08/spring-data-auditoraware/
相关推荐
主要介绍了基于Spring Data的AuditorAware审计功能的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring Data JPA API。 Spring Data JPA 开发文档。 官网 Spring Data JPA API。
Spring Data MongoDB API。 Spring Data MongoDB 开发文档。
本次课程以SpringData为中心,重点讲解了其JPA组件,扩展讲解了redis,mongDB,ES组件,并且对部分组件做了必要的源码分析。而且在课程的最后部分加入了一个综合案例,可以将前面章节所学知识点应用到一个项目中,帮助...
基于SpringBoot+Spring Data JPA+mybatis的仓库管理系统 基于SpringBoot+Spring Data JPA+mybatis的仓库管理系统 基于SpringBoot+Spring Data JPA+mybatis的仓库管理系统 基于SpringBoot+Spring Data JPA+mybatis的...
1. 尚硅谷_SpringData_概述 2. 尚硅谷_SpringData_HelloWorld 3. 尚硅谷_SpringData_Repository接口 4. 尚硅谷_SpringData_Repository查询方法定义规范 5. 尚硅谷_SpringData_Query注解 6. 尚硅谷_SpringData_...
1、基于SpringBoot+Spring Data JPA+mybatis的仓库管理系统源码.zip 2、该资源包括项目的全部源码,下载可以直接使用! 3、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考...
spring-data-jest, Jest的Spring Data 实现 Spring Data Jest 基于on客户端的ElasticSearch的Spring Data 实现仅在 HTTP ( 例如AWS上) 可以访问的情况下使用 Spring Data 和ElasticSearch群集。
在线教育平台服务端基于Spring Boot构建,采用Spring Cloud微服务框架。 持久层:MySQL、MongoDB、Redis、ElasticSearch 数据访问层:使用Spring Data JPA 、Mybatis、Spring Data Mongodb等 业务层:Spring IOC、...
'SpringDataJPA从入门到精通'以SpringBoot为技术基础 从入门到精通 由浅入深地介绍SpringDataJPA的使用。有语法 有实践 有原理剖析。'SpringDataJPA从入门到精通'分为12章 内容包括整体认识JPA、JPA基础查询方法、...
采用目前主流的微服务系统架构 SpringBoot+SpringCloud+SpringData进行开发,前端技术采用Vue.js 。系统整体分为 三大部分:微服务、网站前台、网站管理后台。功能模块包括文章、问答、招聘、活 动、吐槽、交友、...
Spring Data JPA中文文档1.4.3
1. The Spring Data Project 2. Repositories: Convenient Data Access Layers 3. Type-Safe Querying Using Querydsl . . Part II. Relational Databases 4. JPA Repositories 5. Type-Safe JDBC Programming with ...
本文档详细介绍了Spring data JPA的概念、Spring Data JPA能够做什么,Services和 Repositories的对比以及各种Repositories的使用方法。
Spring Data MongoDB中文文档 便宜下载了。
Spring Data 项目的目的是为了简化构建基于 Spring 框架应用的数据访问计数,包括非关系数据库、Map-Reduce 框架、云数据服务等等;另外也包含对关系数据库的访问支持。Spring Data 包含多个子项目:Commons - 提供...
(完整版)基于SpringCloud微服务系统设计方案.docx(完整版)基于SpringCloud微服务系统设计方案.docx(完整版)基于SpringCloud微服务系统设计方案.docx(完整版)基于SpringCloud微服务系统设计方案.docx(完整版)基于...
spring注解完整版+spring data jpa官方文档中文翻译+JPA2.0官方文档 文档内容齐全 值得参考学习
(完整版)基于SpringCloud微服务系统设计方案.pdf(完整版)基于SpringCloud微服务系统设计方案.pdf(完整版)基于SpringCloud微服务系统设计方案.pdf(完整版)基于SpringCloud微服务系统设计方案.pdf(完整版)基于Spring...
《Spring Data实战》介绍了Spring Data、Repository、Querydsl的基础理念,然后阐述了借助Spring Data如何简化NoSQL和大数据的访问,并且涵盖了使用Spring Roo和Spring Data REST导出功能如何实现应用的快速开发,除...