溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

基于Spring Data的AuditorAware審計功能的示例代碼

發(fā)布時間:2020-09-10 13:00:03 來源:腳本之家 閱讀:270 作者:智頂筆記 欄目:編程語言

Spring Data提供支持審計功能:即由誰在什么時候創(chuàng)建或修改實體。Spring Data提供了在實體類的屬性上增加@CreatedBy,@LastModifiedBy,@CreatedDate,@LastModifiedDate注解,并配置相應的配置項,即可實現審計功能,有系統(tǒng)自動記錄 createdBy CreatedDate lastModifiedBy lastModifiedDate 四個屬性的值,下面為具體的配置項。

示例

創(chuàng)建一個實體類

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();
}

創(chuàng)建相應的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的主要功能是將當前執(zhí)行的進程和SecurityContext關聯起來。

SecurityContextHolder.MODE_INHERITABLETHREADLOCAL :用于線程有父子關系的情景中,子線程集成父線程的SecurityContextHolder;

SecurityContextHolder.MODE_INHERITABLETHREADLOCAL :全局共用SecurityContextHolder。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI