Parcourir la source

增加自动填充属性

lishuangjiang@potevio.com il y a 1 an
Parent
commit
758e57ca34

+ 9 - 2
iot-starter/src/main/java/cc/iotkit/Application.java

@@ -11,12 +11,14 @@ package cc.iotkit;
 
 import cc.iotkit.config.EmbeddedElasticSearchConfig;
 import cc.iotkit.config.EmbeddedRedisConfig;
+import cc.iotkit.config.UserAuditor;
 import com.gitee.starblues.loader.DevelopmentMode;
 import com.gitee.starblues.loader.launcher.SpringBootstrap;
 import com.gitee.starblues.loader.launcher.SpringMainBootstrap;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
 import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
 import org.springframework.transaction.annotation.EnableTransactionManagement;
 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@@ -25,11 +27,11 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 @SpringBootApplication
 @EnableTransactionManagement
 @EnableWebMvc
-@EnableJpaAuditing
+@EnableJpaAuditing(auditorAwareRef = "userAuditor")
 public class Application implements SpringBootstrap {
 
     public static void main(String[] args) {
-//        System.setProperty("disabledEmbeddedEs", "true");
+        System.setProperty("disabledEmbeddedEs", "true");
         System.setProperty("disabledEmbeddedRedis", "true");
         if (EmbeddedElasticSearchConfig.embeddedEnable()) {
             EmbeddedElasticSearchConfig.startEmbeddedElasticSearch();
@@ -51,4 +53,9 @@ public class Application implements SpringBootstrap {
     public String developmentMode() {
         return DevelopmentMode.ISOLATION;
     }
+
+    @Bean
+    public UserAuditor setUserAuditorAware(){
+        return new UserAuditor();
+    }
 }

+ 30 - 0
iot-starter/src/main/java/cc/iotkit/config/UserAuditor.java

@@ -0,0 +1,30 @@
+package cc.iotkit.config;
+
+import cc.iotkit.common.satoken.utils.LoginHelper;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.domain.AuditorAware;
+
+import java.util.Objects;
+import java.util.Optional;
+
+@Configuration
+@Slf4j
+public class UserAuditor implements AuditorAware<Long> {
+
+    /**
+     * 获取当前创建或修改的用户
+     * @return
+     */
+    @Override
+    public Optional<Long> getCurrentAuditor() {
+        Long userId = LoginHelper.getUserId();
+
+        if (Objects.nonNull(userId)) {
+            return Optional.of(userId);
+        } else {
+            return Optional.empty();
+        }
+    }
+}
+