From e1929aa4c5e4bde1db50fc9eb0552f2272cb32cd Mon Sep 17 00:00:00 2001
From: houzhongjian <houzhongyi@126.com>
Date: 星期三, 24 七月 2024 15:25:54 +0800
Subject: [PATCH] 1、请求租户不存在时,记录访问日志、操作日志异常的问题 2、LoginUser 新增过期时间,方便判断 token 过期 3、增加 ConfigApi,支持参数配置的读取

---
 iailab-cloud/iailab-gateway/src/main/java/com/iailab/gateway/filter/security/TokenAuthenticationFilter.java |    5 +-
 iailab-module-infra/iailab-module-infra-api/src/main/java/com/iailab/module/infra/api/config/ConfigApi.java |    5 ++
 iailab-cloud/iailab-gateway/src/main/java/com/iailab/gateway/jackson/JacksonAutoConfiguration.java          |   51 +++++++++++++++++++++++++
 iailab-module-system/iailab-module-system-biz/src/main/resources/application.yaml                           |    2 
 4 files changed, 59 insertions(+), 4 deletions(-)

diff --git a/iailab-cloud/iailab-gateway/src/main/java/com/iailab/gateway/filter/security/TokenAuthenticationFilter.java b/iailab-cloud/iailab-gateway/src/main/java/com/iailab/gateway/filter/security/TokenAuthenticationFilter.java
index 499145f..a144b2c 100644
--- a/iailab-cloud/iailab-gateway/src/main/java/com/iailab/gateway/filter/security/TokenAuthenticationFilter.java
+++ b/iailab-cloud/iailab-gateway/src/main/java/com/iailab/gateway/filter/security/TokenAuthenticationFilter.java
@@ -33,7 +33,7 @@
  * 1. 验证通过时,将 userId、userType、tenantId 通过 Header 转发给服务
  * 2. 验证不通过,还是会转发给服务。因为,接口是否需要登录的校验,还是交给服务自身处理
  *
- * @author iailab
+ * @author 芋道源码
  */
 @Component
 public class TokenAuthenticationFilter implements GlobalFilter, Ordered {
@@ -96,7 +96,7 @@
         return getLoginUser(exchange, token).defaultIfEmpty(LOGIN_USER_EMPTY).flatMap(user -> {
             // 1. 无用户,直接 filter 继续请求
             if (user == LOGIN_USER_EMPTY || // 下面 expiresTime 的判断,为了解决 token 实际已经过期的情况
-                    user.getExpiresTime() == null || LocalDateTimeUtils.afterNow(user.getExpiresTime())) {
+                    user.getExpiresTime() == null || LocalDateTimeUtils.beforeNow(user.getExpiresTime())) {
                 return chain.filter(exchange);
             }
 
@@ -163,5 +163,4 @@
     public int getOrder() {
         return -100; // 和 Spring Security Filter 的顺序对齐
     }
-
 }
diff --git a/iailab-cloud/iailab-gateway/src/main/java/com/iailab/gateway/jackson/JacksonAutoConfiguration.java b/iailab-cloud/iailab-gateway/src/main/java/com/iailab/gateway/jackson/JacksonAutoConfiguration.java
new file mode 100644
index 0000000..e6d532c
--- /dev/null
+++ b/iailab-cloud/iailab-gateway/src/main/java/com/iailab/gateway/jackson/JacksonAutoConfiguration.java
@@ -0,0 +1,51 @@
+package com.iailab.gateway.jackson;
+
+import cn.hutool.core.collection.CollUtil;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
+import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
+import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
+import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
+import com.iailab.framework.common.util.json.JsonUtils;
+import com.iailab.framework.common.util.json.databind.NumberSerializer;
+import com.iailab.framework.common.util.json.databind.TimestampLocalDateTimeDeserializer;
+import com.iailab.framework.common.util.json.databind.TimestampLocalDateTimeSerializer;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.List;
+
+@Configuration
+@Slf4j
+public class JacksonAutoConfiguration {
+
+    @Bean
+    public JsonUtils jsonUtils(List<ObjectMapper> objectMappers) {
+        // 1.1 创建 SimpleModule 对象
+        SimpleModule simpleModule = new SimpleModule();
+        simpleModule
+                // 新增 Long 类型序列化规则,数值超过 2^53-1,在 JS 会出现精度丢失问题,因此 Long 自动序列化为字符串类型
+                .addSerializer(Long.class, NumberSerializer.INSTANCE)
+                .addSerializer(Long.TYPE, NumberSerializer.INSTANCE)
+                .addSerializer(LocalDate.class, LocalDateSerializer.INSTANCE)
+                .addDeserializer(LocalDate.class, LocalDateDeserializer.INSTANCE)
+                .addSerializer(LocalTime.class, LocalTimeSerializer.INSTANCE)
+                .addDeserializer(LocalTime.class, LocalTimeDeserializer.INSTANCE)
+                // 新增 LocalDateTime 序列化、反序列化规则,使用 Long 时间戳
+                .addSerializer(LocalDateTime.class, TimestampLocalDateTimeSerializer.INSTANCE)
+                .addDeserializer(LocalDateTime.class, TimestampLocalDateTimeDeserializer.INSTANCE);
+        // 1.2 注册到 objectMapper
+        objectMappers.forEach(objectMapper -> objectMapper.registerModule(simpleModule));
+
+        // 2. 设置 objectMapper 到 JsonUtils
+        JsonUtils.init(CollUtil.getFirst(objectMappers));
+        log.info("[init][初始化 JsonUtils 成功]");
+        return new JsonUtils();
+    }
+
+}
diff --git a/iailab-module-infra/iailab-module-infra-api/src/main/java/com/iailab/module/infra/api/config/ConfigApi.java b/iailab-module-infra/iailab-module-infra-api/src/main/java/com/iailab/module/infra/api/config/ConfigApi.java
index d1f4dd2..e386926 100644
--- a/iailab-module-infra/iailab-module-infra-api/src/main/java/com/iailab/module/infra/api/config/ConfigApi.java
+++ b/iailab-module-infra/iailab-module-infra-api/src/main/java/com/iailab/module/infra/api/config/ConfigApi.java
@@ -13,6 +13,11 @@
 
     String PREFIX = ApiConstants.PREFIX + "/config";
 
+    @Operation(summary = "Feign接口-查询配置参数")
+    @GetMapping("/api/feign/infra-config/{configCode}")
+    String queryConfigByCode(@PathVariable("configCode") String configCode);
+
+
     @GetMapping(PREFIX + "/get-value-by-key")
     @Operation(summary = "根据参数键查询参数值")
     CommonResult<String> getConfigValueByKey(@RequestParam("key") String key);
diff --git a/iailab-module-system/iailab-module-system-biz/src/main/resources/application.yaml b/iailab-module-system/iailab-module-system-biz/src/main/resources/application.yaml
index 3ccc005..1ec8ca2 100644
--- a/iailab-module-system/iailab-module-system-biz/src/main/resources/application.yaml
+++ b/iailab-module-system/iailab-module-system-biz/src/main/resources/application.yaml
@@ -167,7 +167,7 @@
     description: 提供管理员管理的所有功能
     version: ${iailab.info.version}
   tenant: # 多租户相关配置项
-    enable: true
+    enable: false
     ignore-urls:
       - /admin-api/system/tenant/get-id-by-name # 基于名字获取租户,不许带租户编号
       - /admin-api/system/tenant/get-by-website # 基于域名获取租户,不许带租户编号

--
Gitblit v1.9.3