基于Spring Data的AuditorAware审计功能的示例代码

 更新时间:2018年03月08日 14:16:49   作者:智顶笔记  
这篇文章主要介绍了基于Spring Data的AuditorAware审计功能的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java转JSON串的几种方式

    Java转JSON串的几种方式

    本文给大家总结一下java转json串的几种方式,每种方式通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随脚本之家小编一起学习吧
    2018-05-05
  • mybatis-plus添加数据时id自增问题及解决

    mybatis-plus添加数据时id自增问题及解决

    这篇文章主要介绍了mybatis-plus添加数据时id自增问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Java加解密技术系列之RSA详解

    Java加解密技术系列之RSA详解

    出于安全考虑,网络的传输中经常对传输数据做加密和编码处理,本篇文章主要介绍Java加解密技术系列之RSA详解,非常具有实用价值,需要的朋友可以参考下。
    2016-10-10
  • struts2的流程和一系列相关知识代码解析

    struts2的流程和一系列相关知识代码解析

    这篇文章主要介绍了struts2的流程和一系列相关知识代码解析,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • 教你使用eclipse 搭建Swt 环境的全过程

    教你使用eclipse 搭建Swt 环境的全过程

    本文给大家分享使用eclipse 搭建Swt 环境的全过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-12-12
  • Java利用Strategy模式实现堆排序

    Java利用Strategy模式实现堆排序

    策略设计模式(Strategy):可以整体的替换一个算法的实现部分,能够整体的替换算法,能让我们轻松地用不同方法解决同一个问题。本文将利用Strategy模式实现堆排序,感兴趣的可以学习一下
    2022-09-09
  • 手把手带你分析SpringBoot自动装配完成了Ribbon哪些核心操作

    手把手带你分析SpringBoot自动装配完成了Ribbon哪些核心操作

    这篇文章主要介绍了详解Spring Boot自动装配Ribbon哪些核心操作的哪些操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-08-08
  • Java计算时间差和日期差五种常用示例

    Java计算时间差和日期差五种常用示例

    这篇文章主要给大家介绍了关于Java计算时间差和日期差五种常用示例的相关资料,最近工作中遇到需要计算时间差和日期差,搜索了几种计算时间差和日期差的方法,这里总结一下,需要的朋友可以参考下
    2023-08-08
  • SpringMVC集成Web与MVC执行流程和数据响应及交互相关介绍全面总结

    SpringMVC集成Web与MVC执行流程和数据响应及交互相关介绍全面总结

    Spring MVC 是 Spring 提供的一个基于 MVC 设计模式的轻量级 Web 开发框架,本质上相当于 Servlet,Spring MVC 角色划分清晰,分工明细,这篇文章主要介绍了SpringMVC集成Web与MVC执行流程和数据响应及交互
    2022-10-10
  • SpringSessionRedis配置及发现的问题讲解

    SpringSessionRedis配置及发现的问题讲解

    今天小编就为大家分享一篇关于SpringSessionRedis配置及发现的问题讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03

最新评论