一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - 基于Spring Data的AuditorAware審計功能的示例代碼

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

2021-04-10 13:00智頂筆記 JAVA教程

這篇文章主要介紹了基于Spring Data的AuditorAware審計功能的示例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

示例

創建一個實體類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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

?
1
2
3
4
5
6
7
8
9
10
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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入口類開啟審計功能

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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獲取用戶的信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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。

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

原文鏈接:http://www.wisely.top/2018/03/08/spring-data-auditoraware

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美女被吸乳老师羞羞漫画 | 明星乱亚洲 | 欧美人做人爱a全程免费 | 草莓茄子丝瓜番茄小蝌蚪 | 北岛玲在线播放 | 日本在线视 | 色人阁图片 | haodiaose在线精品免费视频 | 1769最新资源站 | 黄色wwwwww | 日韩毛片免费线上观看 | 欧美贵妇videos办公室360 | 久久观看视频 | 好男人好资源在线观看免费 | 日韩一区二三区无 | 欧美日韩精品乱国产 | a级特黄毛片免费观看 | 国产福利资源网在线观看 | 乳 好大h| 国色天香社区视频免费观看3 | 99久久一区二区精品 | 久久re热在线视频精99 | 无码中文字幕热热久久 | 国产第一综合另类色区奇米 | 日韩精品特黄毛片免费看 | 日韩中文在线 | 美女脱了内裤打开腿让人羞羞软件 | 国产一区二区三区欧美 | 日韩爱爱 | 欧洲美女bbbxxxxxx| 国产成人一区二区三区视频免费蜜 | 日本一区二区在线不卡 | 973影院| 日韩欧美国产在线 | 高清国语自产拍免费视频国产 | 日本中出视频 | 法国老妇性xx在线播放 | 视频网站入口在线看 | 男人猛激烈吃奶gif动态图 | www.尤物| 久9青青cao精品视频在线 |