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

基于Spring Data的AuditorAware審計(jì)功能的示例代碼

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

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

示例

創(chuàng)建一個(gè)實(shí)體類

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入口類開啟審計(jì)功能

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 保存對(duì)象時(shí), createdBy CreatedDate lastModifiedBy lastModifiedDate 有審計(jì)功能自動(dòng)插入

注:在異步方法中如何獲取用戶信息

由于在異步方法中使用repository保存對(duì)象,獲取不到用戶用戶信息,需增加如下配置項(xiàng),即可在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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java多線程Queue、BlockingQueue和使用BlockingQueue實(shí)現(xiàn)生產(chǎn)消費(fèi)者模型方法解析

    Java多線程Queue、BlockingQueue和使用BlockingQueue實(shí)現(xiàn)生產(chǎn)消費(fèi)者模型方法解析

    這篇文章主要介紹了Java多線程Queue、BlockingQueue和使用BlockingQueue實(shí)現(xiàn)生產(chǎn)消費(fèi)者模型方法解析,涉及queue,BlockingQueue等有關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以參考。
    2017-11-11
  • java多態(tài)性中的Overload和Override區(qū)別詳解

    java多態(tài)性中的Overload和Override區(qū)別詳解

    這篇文章主要介紹了java多態(tài)性中的Overload和Override區(qū)別詳解,重寫(Overriding)是父類與子類之間多態(tài)性的一種表現(xiàn),而重載(Overloading)是一個(gè)類中多態(tài)性的一種表現(xiàn),需要的朋友可以參考下
    2023-07-07
  • java九種分布式ID解決方案

    java九種分布式ID解決方案

    在日常的業(yè)務(wù)開發(fā)中,通常需要對(duì)一些數(shù)據(jù)做唯一標(biāo)識(shí),本文主要介紹了java九種分布式ID解決方案,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • SpringBoot實(shí)現(xiàn)文件在線預(yù)覽功能的全過程

    SpringBoot實(shí)現(xiàn)文件在線預(yù)覽功能的全過程

    我們開發(fā)業(yè)務(wù)系統(tǒng)的時(shí)候,經(jīng)常有那種文檔文件在線預(yù)覽的需求,下面這篇文章主要給大家介紹了關(guān)于SpringBoot實(shí)現(xiàn)文件在線預(yù)覽功能的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • 在zuulFilter中注入bean失敗的解決方案

    在zuulFilter中注入bean失敗的解決方案

    這篇文章主要介紹了在zuulFilter中注入bean失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Swagger2不被SpringSecurity框架攔截的配置及說明

    Swagger2不被SpringSecurity框架攔截的配置及說明

    這篇文章主要介紹了Swagger2不被SpringSecurity框架攔截的配置及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Springboot集成spring data elasticsearch過程詳解

    Springboot集成spring data elasticsearch過程詳解

    這篇文章主要介紹了springboot集成spring data elasticsearch過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 從Java到JSON一起探索Jackson的魔力

    從Java到JSON一起探索Jackson的魔力

    Jackson是一個(gè)用于處理JSON數(shù)據(jù)的開源Java庫,這篇文章主要為大家介紹了Java是如何利用Jackson處理JSON數(shù)據(jù)的,感興趣的小伙伴可以了解一下
    2023-05-05
  • 深入學(xué)習(xí)Java中的SPI機(jī)制

    深入學(xué)習(xí)Java中的SPI機(jī)制

    這篇文章主要介紹了深入學(xué)習(xí)Java中的SPI機(jī)制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • 詳解Java中hashCode的作用

    詳解Java中hashCode的作用

    這篇文章主要介紹了詳解Java中hashCode的作用的相關(guān)資料,需要的朋友可以參考下
    2017-03-03

最新評(píng)論

沧源| 思南县| 长武县| 泗洪县| 曲麻莱县| 田阳县| 睢宁县| 青海省| 六枝特区| 铜梁县| 永定县| 土默特左旗| 高雄县| 广南县| 梁山县| 会东县| 伊春市| 武威市| 英超| 乌拉特中旗| 习水县| 玛曲县| 琼中| 满洲里市| 信宜市| 尼木县| 商城县| 北碚区| 九龙坡区| 驻马店市| 资兴市| 汤阴县| 武邑县| 大邑县| 香港| 永寿县| 永仁县| 藁城市| 三穗县| 成都市| 彩票|