token 解密

This commit is contained in:
cwchen 2025-09-29 15:43:03 +08:00
parent 6d6d6cf5ea
commit 8274d8fd70
2 changed files with 70 additions and 1 deletions

View File

@ -79,4 +79,9 @@ public class CacheConstants
* */
public static final String REPLAY_ATTACK ="replayAttack";
/**
* auth是否需要解密
* */
public static final String AUTH ="auth";
}

View File

@ -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<SystemConfigVo> 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<SystemConfigVo> 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;
}
}