基于Spring Data的AuditorAware审计功能的示例代码
SpringData提供支持审计功能:即由谁在什么时候创建或修改实体。SpringData提供了在实体类的属性上增加@CreatedBy,@LastModifiedBy,@CreatedDate,@LastModifiedDate注解,并配置相应的配置项,即可实现审计功能,有系统自动记录createdByCreatedDatelastModifiedBylastModifiedDate四个属性的值,下面为具体的配置项。
示例
创建一个实体类
packagecom.hfcsbc.infrastructureservice.domain; importcom.hfcsbc.repository.support.domain.AbstractAuditingEntity; importlombok.Data; importorg.springframework.data.annotation.CreatedBy; importorg.springframework.data.annotation.CreatedDate; importorg.springframework.data.annotation.LastModifiedBy; importorg.springframework.data.annotation.LastModifiedDate; importorg.springframework.data.jpa.domain.support.AuditingEntityListener; importjavax.persistence.*; importjava.util.Date; /** *Createbypengchaoon2018/3/7 */ @Entity @Data @EntityListeners({AuditingEntityListener.class}) publicclassPerson{ @Id @GeneratedValue privateLongid; privateStringname; privateIntegerage; @CreatedBy @Column( name="created_by", nullable=false, length=50, updatable=false ) privateStringcreatedBy; @CreatedDate @Column( name="created_date", nullable=false, updatable=false ) privateDatecreatedDate=newDate(); @LastModifiedBy @Column( name="last_modified_by", length=50 ) privateStringlastModifiedBy; @LastModifiedDate @Column( name="last_modified_date" ) privateDatelastModifiedDate=newDate(); }
创建相应的Repository
packagecom.hfcsbc.repository; importcom.hfcsbc.domain.Person; importorg.springframework.data.jpa.repository.JpaRepository; /** *Createbypengchaoon2018/3/7 */ publicinterfacePersonRepositoryextendsJpaRepository{ }
配置获取用户信息的bean
packagecom.hfcsbc.infrastructureservice.config; importorg.springframework.data.domain.AuditorAware; importorg.springframework.security.core.Authentication; importorg.springframework.security.core.context.SecurityContextHolder; importorg.springframework.stereotype.Component; importjava.util.Optional; /** *Createbypengchaoon2018/3/7 */ @Component("auditorAware") publicclassAuditorAwareImplimplementsAuditorAware{ @Override publicOptional getCurrentAuditor(){ Authenticationauthentication=SecurityContextHolder.getContext().getAuthentication(); returnOptional.of(authentication.getPrincipal().toString()); } }
在SpringBoot入口类开启审计功能
packagecom.hfcsbc.infrastructureservice; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; importorg.springframework.data.jpa.repository.config.EnableJpaAuditing; importorg.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication @EnableJpaAuditing(auditorAwareRef="auditorAware") @EnableAsync publicclassPersonApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(PersonApplication.class,args); } }
即完成配置,在使用repository保存对象时,createdByCreatedDatelastModifiedBylastModifiedDate有审计功能自动插入
注:在异步方法中如何获取用户信息
由于在异步方法中使用repository保存对象,获取不到用户用户信息,需增加如下配置项,即可在Authentication获取用户的信息
packagecom.hfcsbc.config; importorg.springframework.beans.factory.config.MethodInvokingFactoryBean; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importorg.springframework.security.core.context.SecurityContextHolder; /** *Createbypengchaoon2018/3/7 */ @Configuration publicclassAuditorAwareConfig{ @Bean publicMethodInvokingFactoryBeanmethodInvokingFactoryBean(){ MethodInvokingFactoryBeanmethodInvokingFactoryBean=newMethodInvokingFactoryBean(); methodInvokingFactoryBean.setTargetClass(SecurityContextHolder.class); methodInvokingFactoryBean.setTargetMethod("setStrategyName"); methodInvokingFactoryBean.setArguments(newString[]{SecurityContextHolder.MODE_INHERITABLETHREADLOCAL}); returnmethodInvokingFactoryBean; } }
SecurityContextHolder的主要功能是将当前执行的进程和SecurityContext关联起来。
SecurityContextHolder.MODE_INHERITABLETHREADLOCAL:用于线程有父子关系的情景中,子线程集成父线程的SecurityContextHolder;
SecurityContextHolder.MODE_INHERITABLETHREADLOCAL:全局共用SecurityContextHolder。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。