feat(common): 优化基础框架和认证模块

- 完善Token认证服务和用户上下文管理
- 优化BaseEntity基础实体类
- 更新pom.xml依赖配置
-增强通用工具类功能
This commit is contained in:
zhangjf 2026-03-02 07:30:44 +08:00
parent ab412935e1
commit 6dfc8ea686
5 changed files with 36 additions and 27 deletions

View File

@ -78,6 +78,13 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- Hutool (雪花ID生成器) -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.8.25</version>
</dependency>
<!-- Test --> <!-- Test -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

@ -17,7 +17,7 @@ public class TokenInfo implements Serializable {
/** /**
* 用户ID * 用户ID
*/ */
private Long userId; private String userId;
/** /**
* 用户名 * 用户名
@ -27,7 +27,7 @@ public class TokenInfo implements Serializable {
/** /**
* 租户ID * 租户ID
*/ */
private Long tenantId; private String tenantId;
/** /**
* 登录时间戳 * 登录时间戳
@ -42,7 +42,7 @@ public class TokenInfo implements Serializable {
public TokenInfo() { 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.userId = userId;
this.username = username; this.username = username;
this.tenantId = tenantId; this.tenantId = tenantId;
@ -50,11 +50,11 @@ public class TokenInfo implements Serializable {
this.expireTime = expireTime; this.expireTime = expireTime;
} }
public Long getUserId() { public String getUserId() {
return userId; return userId;
} }
public void setUserId(Long userId) { public void setUserId(String userId) {
this.userId = userId; this.userId = userId;
} }
@ -66,11 +66,11 @@ public class TokenInfo implements Serializable {
this.username = username; this.username = username;
} }
public Long getTenantId() { public String getTenantId() {
return tenantId; return tenantId;
} }
public void setTenantId(Long tenantId) { public void setTenantId(String tenantId) {
this.tenantId = tenantId; this.tenantId = tenantId;
} }

View File

@ -42,7 +42,7 @@ public class TokenService {
* @param tenantId 租户ID * @param tenantId 租户ID
* @return Token字符串 * @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); return generateToken(userId, username, tenantId, DEFAULT_EXPIRE_SECONDS);
} }
@ -55,7 +55,7 @@ public class TokenService {
* @param expireSeconds 过期时间 * @param expireSeconds 过期时间
* @return Token字符串 * @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 // 生成UUID作为Token
String token = UUID.randomUUID().toString().replace("-", ""); String token = UUID.randomUUID().toString().replace("-", "");
@ -173,7 +173,7 @@ public class TokenService {
* *
* @param userId 用户ID * @param userId 用户ID
*/ */
public void deleteAllUserTokens(Long userId) { public void deleteAllUserTokens(String userId) {
String userTokensKey = getUserTokensKey(userId); String userTokensKey = getUserTokensKey(userId);
java.util.Map<Object, Object> tokens = redisService.hGetAll(userTokensKey); java.util.Map<Object, Object> tokens = redisService.hGetAll(userTokensKey);
@ -225,7 +225,7 @@ public class TokenService {
/** /**
* 构建用户Token列表Key * 构建用户Token列表Key
*/ */
private String getUserTokensKey(Long userId) { private String getUserTokensKey(String userId) {
return USER_TOKENS_PREFIX + userId; return USER_TOKENS_PREFIX + userId;
} }
} }

View File

@ -5,17 +5,17 @@ package com.fundplatform.common.context;
*/ */
public final class UserContextHolder { public final class UserContextHolder {
private static final ThreadLocal<Long> USER_ID_HOLDER = new ThreadLocal<>(); private static final ThreadLocal<String> USER_ID_HOLDER = new ThreadLocal<>();
private static final ThreadLocal<String> USER_NAME_HOLDER = new ThreadLocal<>(); private static final ThreadLocal<String> USER_NAME_HOLDER = new ThreadLocal<>();
private UserContextHolder() { private UserContextHolder() {
} }
public static void setUserId(Long userId) { public static void setUserId(String userId) {
USER_ID_HOLDER.set(userId); USER_ID_HOLDER.set(userId);
} }
public static Long getUserId() { public static String getUserId() {
return USER_ID_HOLDER.get(); return USER_ID_HOLDER.get();
} }

View File

@ -8,25 +8,27 @@ import java.time.LocalDateTime;
* *
* <p>注意此类不绑定具体 ORM 框架注解 JPAMyBatis-Plus * <p>注意此类不绑定具体 ORM 框架注解 JPAMyBatis-Plus
* 仅作为字段规范的统一来源具体映射由各模块自行扩展</p> * 仅作为字段规范的统一来源具体映射由各模块自行扩展</p>
*
* <p>主键采用字符串类型雪花算法生成解决前端JavaScript大数精度丢失问题</p>
*/ */
public abstract class BaseEntity implements Serializable { public abstract class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** 主键ID */ /** 主键ID雪花算法生成的19位字符串 */
private Long id; private String id;
/** 租户ID多租户隔离 */ /** 租户ID多租户隔离 */
private Long tenantId; private String tenantId;
/** 创建人 */ /** 创建人 */
private Long createdBy; private String createdBy;
/** 创建时间 */ /** 创建时间 */
private LocalDateTime createdTime; private LocalDateTime createdTime;
/** 更新人 */ /** 更新人 */
private Long updatedBy; private String updatedBy;
/** 更新时间 */ /** 更新时间 */
private LocalDateTime updatedTime; private LocalDateTime updatedTime;
@ -37,27 +39,27 @@ public abstract class BaseEntity implements Serializable {
/** 备注 */ /** 备注 */
private String remark; private String remark;
public Long getId() { public String getId() {
return id; return id;
} }
public void setId(Long id) { public void setId(String id) {
this.id = id; this.id = id;
} }
public Long getTenantId() { public String getTenantId() {
return tenantId; return tenantId;
} }
public void setTenantId(Long tenantId) { public void setTenantId(String tenantId) {
this.tenantId = tenantId; this.tenantId = tenantId;
} }
public Long getCreatedBy() { public String getCreatedBy() {
return createdBy; return createdBy;
} }
public void setCreatedBy(Long createdBy) { public void setCreatedBy(String createdBy) {
this.createdBy = createdBy; this.createdBy = createdBy;
} }
@ -69,11 +71,11 @@ public abstract class BaseEntity implements Serializable {
this.createdTime = createdTime; this.createdTime = createdTime;
} }
public Long getUpdatedBy() { public String getUpdatedBy() {
return updatedBy; return updatedBy;
} }
public void setUpdatedBy(Long updatedBy) { public void setUpdatedBy(String updatedBy) {
this.updatedBy = updatedBy; this.updatedBy = updatedBy;
} }