From 8274d8fd70bfb8e92c99db75dec9f67fb68a6c67 Mon Sep 17 00:00:00 2001 From: cwchen <1048842385@qq.com> Date: Mon, 29 Sep 2025 15:43:03 +0800 Subject: [PATCH] =?UTF-8?q?token=20=E8=A7=A3=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bonus/common/constant/CacheConstants.java | 5 ++ .../framework/web/service/TokenService.java | 66 ++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/bonus-common/src/main/java/com/bonus/common/constant/CacheConstants.java b/bonus-common/src/main/java/com/bonus/common/constant/CacheConstants.java index 2b8089c..46756e6 100644 --- a/bonus-common/src/main/java/com/bonus/common/constant/CacheConstants.java +++ b/bonus-common/src/main/java/com/bonus/common/constant/CacheConstants.java @@ -79,4 +79,9 @@ public class CacheConstants * */ public static final String REPLAY_ATTACK ="replayAttack"; + /** + * auth是否需要解密 + * */ + public static final String AUTH ="auth"; + } diff --git a/bonus-framework/src/main/java/com/bonus/framework/web/service/TokenService.java b/bonus-framework/src/main/java/com/bonus/framework/web/service/TokenService.java index e705256..86cc658 100644 --- a/bonus-framework/src/main/java/com/bonus/framework/web/service/TokenService.java +++ b/bonus-framework/src/main/java/com/bonus/framework/web/service/TokenService.java @@ -1,11 +1,17 @@ package com.bonus.framework.web.service; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.concurrent.TimeUnit; import javax.servlet.http.HttpServletRequest; +import com.alibaba.fastjson2.JSON; import com.bonus.common.utils.encryption.Sm4Utils; +import com.bonus.system.domain.vo.SystemConfigVo; +import com.bonus.system.service.ISystemConfigService; +import org.apache.commons.collections4.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -56,6 +62,13 @@ public class TokenService @Autowired private RedisCache redisCache; + @Autowired + private ISystemConfigService configService; + + private static final long TIMESTAMP_TOLERANCE = 15 * 60 * 1000; // 15分钟 + // 请求签名在Redis中的过期时间(秒) + private static final int SIGNATURE_EXPIRE_SECONDS = (int) (TIMESTAMP_TOLERANCE * 2 / 1000); + /** * 获取用户身份信息 * @@ -278,8 +291,13 @@ public class TokenService if(StringUtils.isEmpty(token)){ return token; }else{ + boolean systemConfigStatus = getSystemConfigStatus(CacheConstants.AUTH); String decryptToken = Sm4Utils.decrypt(token); - return decryptToken; + if(!systemConfigStatus && Objects.equals(decryptToken, token)){ + return token; + }else{ + return decryptToken; + } } } @@ -408,4 +426,50 @@ public class TokenService return false; } } + + /** + * 获取系统配置 + * @return boolean + * @author cwchen + * @date 2025/9/28 10:36 + */ + public boolean getSystemConfigStatus(String key) { + boolean SystemConfigStatus = false; + Object cacheObject = redisCache.getCacheObject(CacheConstants.SYSTEM_CONFIG_VOS); + if(Objects.isNull(cacheObject)){ + List systemConfigVos = configService.listConfig(); + Boolean stored = redisCache.setNxCacheObject(CacheConstants.SYSTEM_CONFIG_VOS, + JSON.toJSONString(systemConfigVos), + (long) SIGNATURE_EXPIRE_SECONDS, + TimeUnit.SECONDS); + if(CollectionUtils.isNotEmpty(systemConfigVos)){ + SystemConfigVo config = systemConfigVos.stream() + .filter(item -> key.equals(item.getConfigCode())) + .findFirst() + .orElse(null); + if(Objects.nonNull(config)){ + String useStatus = config.getUseStatus(); + if(Objects.equals("0",useStatus)){ + SystemConfigStatus = true; + } + } + } + }else{ + String cacheStr = (String) cacheObject; + List systemConfigVos = JSON.parseArray(cacheStr, SystemConfigVo.class); + if(CollectionUtils.isNotEmpty(systemConfigVos)){ + SystemConfigVo config = systemConfigVos.stream() + .filter(item -> key.equals(item.getConfigCode())) + .findFirst() + .orElse(null); + if(Objects.nonNull(config)){ + String useStatus = config.getUseStatus(); + if(Objects.equals("0",useStatus)){ + SystemConfigStatus = true; + } + } + } + } + return SystemConfigStatus; + } } \ No newline at end of file