1
0

feat(web): 添加实体审计功能并配置审计用户

- 在 SimpleEntity 中添加 @CreatedBy 和 @LastModifiedBy 注解
- 在 WebApplication 中添加 AuditorAware Bean
- 使用 UserService 的 currentLoginUserOptional 方法获取当前登录用户
This commit is contained in:
2024-12-05 17:54:58 +08:00
parent a6c128da64
commit 9d6e977d93
2 changed files with 12 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package com.eshore.gringotts.web;
import com.blinkfox.fenix.EnableFenix;
import com.eshore.gringotts.web.domain.user.entity.User;
import com.eshore.gringotts.web.domain.user.service.UserService;
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.springframework.boot.ApplicationArguments;
@@ -9,6 +10,8 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;
@@ -41,4 +44,9 @@ public class WebApplication implements ApplicationRunner {
// 初始化系统管理员
userService.initial();
}
@Bean
public AuditorAware<User> auditorAware(UserService userService) {
return userService::currentLoginUserOptional;
}
}

View File

@@ -12,7 +12,9 @@ import javax.persistence.OneToOne;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
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;
@@ -33,11 +35,13 @@ public class SimpleEntity extends IdOnlyEntity {
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
@ToString.Exclude
@CreatedBy
private User createdUser;
@LastModifiedDate
private LocalDateTime modifiedTime;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
@ToString.Exclude
@LastModifiedBy
private User modifiedUser;
}