From 6dfc8ea686c51e7de1e9c8dcc2d081a4ecef8a53 Mon Sep 17 00:00:00 2001 From: zhangjf Date: Mon, 2 Mar 2026 07:30:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(common):=20=E4=BC=98=E5=8C=96=E5=9F=BA?= =?UTF-8?q?=E7=A1=80=E6=A1=86=E6=9E=B6=E5=92=8C=E8=AE=A4=E8=AF=81=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 完善Token认证服务和用户上下文管理 - 优化BaseEntity基础实体类 - 更新pom.xml依赖配置 -增强通用工具类功能 --- fund-common/pom.xml | 7 +++++ .../fundplatform/common/auth/TokenInfo.java | 14 +++++----- .../common/auth/TokenService.java | 8 +++--- .../common/context/UserContextHolder.java | 6 ++-- .../fundplatform/common/core/BaseEntity.java | 28 ++++++++++--------- 5 files changed, 36 insertions(+), 27 deletions(-) diff --git a/fund-common/pom.xml b/fund-common/pom.xml index bb10f7a..d29d030 100644 --- a/fund-common/pom.xml +++ b/fund-common/pom.xml @@ -78,6 +78,13 @@ provided + + + cn.hutool + hutool-core + 5.8.25 + + org.springframework.boot diff --git a/fund-common/src/main/java/com/fundplatform/common/auth/TokenInfo.java b/fund-common/src/main/java/com/fundplatform/common/auth/TokenInfo.java index 70c25e4..bc50045 100644 --- a/fund-common/src/main/java/com/fundplatform/common/auth/TokenInfo.java +++ b/fund-common/src/main/java/com/fundplatform/common/auth/TokenInfo.java @@ -17,7 +17,7 @@ public class TokenInfo implements Serializable { /** * 用户ID */ - private Long userId; + private String userId; /** * 用户名 @@ -27,7 +27,7 @@ public class TokenInfo implements Serializable { /** * 租户ID */ - private Long tenantId; + private String tenantId; /** * 登录时间戳 @@ -42,7 +42,7 @@ public class TokenInfo implements Serializable { public TokenInfo() { } - public TokenInfo(Long userId, String username, Long tenantId, Long expireTime) { + public TokenInfo(String userId, String username, String tenantId, Long expireTime) { this.userId = userId; this.username = username; this.tenantId = tenantId; @@ -50,11 +50,11 @@ public class TokenInfo implements Serializable { this.expireTime = expireTime; } - public Long getUserId() { + public String getUserId() { return userId; } - public void setUserId(Long userId) { + public void setUserId(String userId) { this.userId = userId; } @@ -66,11 +66,11 @@ public class TokenInfo implements Serializable { this.username = username; } - public Long getTenantId() { + public String getTenantId() { return tenantId; } - public void setTenantId(Long tenantId) { + public void setTenantId(String tenantId) { this.tenantId = tenantId; } diff --git a/fund-common/src/main/java/com/fundplatform/common/auth/TokenService.java b/fund-common/src/main/java/com/fundplatform/common/auth/TokenService.java index 15b86fd..e8458fa 100644 --- a/fund-common/src/main/java/com/fundplatform/common/auth/TokenService.java +++ b/fund-common/src/main/java/com/fundplatform/common/auth/TokenService.java @@ -42,7 +42,7 @@ public class TokenService { * @param tenantId 租户ID * @return Token字符串 */ - public String generateToken(Long userId, String username, Long tenantId) { + public String generateToken(String userId, String username, String tenantId) { return generateToken(userId, username, tenantId, DEFAULT_EXPIRE_SECONDS); } @@ -55,7 +55,7 @@ public class TokenService { * @param expireSeconds 过期时间(秒) * @return Token字符串 */ - public String generateToken(Long userId, String username, Long tenantId, long expireSeconds) { + public String generateToken(String userId, String username, String tenantId, long expireSeconds) { // 生成UUID作为Token String token = UUID.randomUUID().toString().replace("-", ""); @@ -173,7 +173,7 @@ public class TokenService { * * @param userId 用户ID */ - public void deleteAllUserTokens(Long userId) { + public void deleteAllUserTokens(String userId) { String userTokensKey = getUserTokensKey(userId); java.util.Map tokens = redisService.hGetAll(userTokensKey); @@ -225,7 +225,7 @@ public class TokenService { /** * 构建用户Token列表Key */ - private String getUserTokensKey(Long userId) { + private String getUserTokensKey(String userId) { return USER_TOKENS_PREFIX + userId; } } diff --git a/fund-common/src/main/java/com/fundplatform/common/context/UserContextHolder.java b/fund-common/src/main/java/com/fundplatform/common/context/UserContextHolder.java index a8d1bae..f19df4f 100644 --- a/fund-common/src/main/java/com/fundplatform/common/context/UserContextHolder.java +++ b/fund-common/src/main/java/com/fundplatform/common/context/UserContextHolder.java @@ -5,17 +5,17 @@ package com.fundplatform.common.context; */ public final class UserContextHolder { - private static final ThreadLocal USER_ID_HOLDER = new ThreadLocal<>(); + private static final ThreadLocal USER_ID_HOLDER = new ThreadLocal<>(); private static final ThreadLocal USER_NAME_HOLDER = new ThreadLocal<>(); private UserContextHolder() { } - public static void setUserId(Long userId) { + public static void setUserId(String userId) { USER_ID_HOLDER.set(userId); } - public static Long getUserId() { + public static String getUserId() { return USER_ID_HOLDER.get(); } diff --git a/fund-common/src/main/java/com/fundplatform/common/core/BaseEntity.java b/fund-common/src/main/java/com/fundplatform/common/core/BaseEntity.java index 14ba34e..abce9e3 100644 --- a/fund-common/src/main/java/com/fundplatform/common/core/BaseEntity.java +++ b/fund-common/src/main/java/com/fundplatform/common/core/BaseEntity.java @@ -8,25 +8,27 @@ import java.time.LocalDateTime; * *

注意:此类不绑定具体 ORM 框架注解(如 JPA、MyBatis-Plus), * 仅作为字段规范的统一来源,具体映射由各模块自行扩展。

+ * + *

主键采用字符串类型(雪花算法生成),解决前端JavaScript大数精度丢失问题。

*/ public abstract class BaseEntity implements Serializable { private static final long serialVersionUID = 1L; - /** 主键ID */ - private Long id; + /** 主键ID(雪花算法生成的19位字符串) */ + private String id; /** 租户ID(多租户隔离) */ - private Long tenantId; + private String tenantId; /** 创建人 */ - private Long createdBy; + private String createdBy; /** 创建时间 */ private LocalDateTime createdTime; /** 更新人 */ - private Long updatedBy; + private String updatedBy; /** 更新时间 */ private LocalDateTime updatedTime; @@ -37,27 +39,27 @@ public abstract class BaseEntity implements Serializable { /** 备注 */ private String remark; - public Long getId() { + public String getId() { return id; } - public void setId(Long id) { + public void setId(String id) { this.id = id; } - public Long getTenantId() { + public String getTenantId() { return tenantId; } - public void setTenantId(Long tenantId) { + public void setTenantId(String tenantId) { this.tenantId = tenantId; } - public Long getCreatedBy() { + public String getCreatedBy() { return createdBy; } - public void setCreatedBy(Long createdBy) { + public void setCreatedBy(String createdBy) { this.createdBy = createdBy; } @@ -69,11 +71,11 @@ public abstract class BaseEntity implements Serializable { this.createdTime = createdTime; } - public Long getUpdatedBy() { + public String getUpdatedBy() { return updatedBy; } - public void setUpdatedBy(Long updatedBy) { + public void setUpdatedBy(String updatedBy) { this.updatedBy = updatedBy; }