欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-25 15:02:07
spring data提供支持审计功能:即由谁在什么时候创建或修改实体。spring data提供了在实体类的属性上增加@createdby,@lastmodifiedby...

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。

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