最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2018年03月08日 14:16:49   作者:智頂筆記  
這篇文章主要介紹了基于Spring Data的AuditorAware審計功能的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Spring Data提供支持審計功能:即由誰在什么時候創(chuàng)建或修改實體。Spring Data提供了在實體類的屬性上增加@CreatedBy,@LastModifiedBy,@CreatedDate,@LastModifiedDate注解,并配置相應(yīng)的配置項,即可實現(xiàn)審計功能,有系統(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)建相應(yī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的主要功能是將當(dāng)前執(zhí)行的進(jìn)程和SecurityContext關(guān)聯(lián)起來。

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

SecurityContextHolder.MODE_INHERITABLETHREADLOCAL :全局共用SecurityContextHolder。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java通過索引值實現(xiàn)約瑟夫環(huán)算法

    Java通過索引值實現(xiàn)約瑟夫環(huán)算法

    這篇文章主要介紹了Java通過索引值實現(xiàn)約瑟夫環(huán),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • java實現(xiàn)動態(tài)時鐘并設(shè)置鬧鐘功能

    java實現(xiàn)動態(tài)時鐘并設(shè)置鬧鐘功能

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)動態(tài)時鐘并設(shè)置鬧鐘功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 部署springboot項目到云服務(wù)器的兩種方式(jar+war)

    部署springboot項目到云服務(wù)器的兩種方式(jar+war)

    本文主要介紹了部署springboot項目到云服務(wù)器的兩種方式,主要介紹了jar和war兩種方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • IDEA如何解決properties文件亂碼問題

    IDEA如何解決properties文件亂碼問題

    IDEA編輯器中打開properties文件時出現(xiàn)中文亂碼,可以通過修改文件編碼格式來解決,具體步驟為:設(shè)置》Setting》Editor》FileEncodings,將編碼格式更改為UTF-8
    2025-01-01
  • Springboot項目因為kackson版本問題啟動報錯解決方案

    Springboot項目因為kackson版本問題啟動報錯解決方案

    這篇文章主要介紹了Springboot項目因為kackson版本問題啟動報錯解決方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • 解決SpringBoot webSocket 資源無法加載、tomcat啟動報錯的問題

    解決SpringBoot webSocket 資源無法加載、tomcat啟動報錯的問題

    這篇文章主要介紹了解決SpringBoot webSocket 資源無法加載、tomcat啟動報錯的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • java jdk1.8 使用stream流進(jìn)行l(wèi)ist 分組歸類操作

    java jdk1.8 使用stream流進(jìn)行l(wèi)ist 分組歸類操作

    這篇文章主要介紹了java jdk1.8 使用stream流進(jìn)行l(wèi)ist 分組歸類操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Java中的Random()函數(shù)及兩種構(gòu)造方法

    Java中的Random()函數(shù)及兩種構(gòu)造方法

    Java中存在著兩種Random函數(shù)分別是java.lang.Math.Random和java.util.Random,文中給大家介紹了random()的兩種構(gòu)造方法,感興趣的朋友跟隨小編一起看看吧
    2018-11-11
  • 微信公眾號服務(wù)號推送模板消息設(shè)置方法(后端java)

    微信公眾號服務(wù)號推送模板消息設(shè)置方法(后端java)

    公眾號時經(jīng)常會需要寫推送消息,從網(wǎng)上找了一大堆,都不是很全,所以這篇文章主要介紹了微信公眾號服務(wù)號推送模板消息設(shè)置方法的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • Spring boot+beetl+i18n國際化處理的方法

    Spring boot+beetl+i18n國際化處理的方法

    這篇文章主要介紹了Spring boot+beetl+i18n國際化處理的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評論

色达县| 灵山县| 旅游| 镇原县| 保康县| 兰溪市| 稻城县| 屏南县| 巧家县| 长乐市| 凤庆县| 新绛县| 沾益县| 甘孜| 高州市| 海盐县| 越西县| 庆城县| 威信县| 卓资县| 漳浦县| 昂仁县| 徐汇区| 城步| 威宁| 滦南县| 栾川县| 泰安市| 祥云县| 北安市| 游戏| 六安市| 淮安市| 孝义市| 宜兰市| 澄江县| 沙洋县| 贺兰县| 呈贡县| 新民市| 潼南县|