token管理,token生成规则修改

This commit is contained in:
liuzhaotian
2025-03-06 14:54:24 +08:00
parent 57a06bd21e
commit 11d1cb198f
@@ -60,7 +60,7 @@ public class TokenConfigServiceImpl implements TokenConfigService{
@Resource
private JwtConfig jwtConfig;
private static final String FIELD_IMES = "imes-";
@Override
public PageResult<TokenConfigRespVO> getTokenConfigPage(TokenConfigPageReqVO pageReqVO) {
@@ -208,7 +208,12 @@ public class TokenConfigServiceImpl implements TokenConfigService{
@Override
public Long checkTokenConfig(String token) {
TokenConfigDO tokenConfigDO = tokenConfigMapper.selectByToken(token);
String substring = token.substring(0, 5);
if(!FIELD_IMES.equals(substring)){
throw exception(TOKEN_DATA_ERROR);
}
TokenConfigDO tokenConfigDO = tokenConfigMapper.selectByToken(token.substring(5));
if(tokenConfigDO == null){
throw exception(TOKEN_CONFIG_NOT_EXISTS);
@@ -243,7 +248,7 @@ public class TokenConfigServiceImpl implements TokenConfigService{
TokenConfigDO tokenConfigDO = validateTokenConfigExists(id);
return tokenConfigDO.getAppToken();
return FIELD_IMES+tokenConfigDO.getAppToken();
}
@@ -285,14 +290,30 @@ public class TokenConfigServiceImpl implements TokenConfigService{
claims.put("organId", organId);
claims.put("mobile", mobile);
claims.put("appType",appType);
claims.put("nowTime",System.currentTimeMillis());
String token = generateToken(claims, base64SecretKey);
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(token.getBytes());
// 使用 SHA-256 处理 生成的 token
MessageDigest shaDigest = MessageDigest.getInstance("SHA-256");
byte[] hash = shaDigest.digest(token.getBytes());
token = Base64.getEncoder().encodeToString(hash);
token = Base64.getEncoder().encodeToString(hash).toLowerCase();
// 使用 MD5 对 token 进行二次处理
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] md5Digest = md.digest(token.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : md5Digest) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0'); // 确保转换后的字符串是两位
}
hexString.append(hex);
}
token = hexString.toString();
}catch (NoSuchAlgorithmException e){
@@ -309,7 +330,7 @@ public class TokenConfigServiceImpl implements TokenConfigService{
public static String maskString(String input) {
int length = input.length();
return input.substring(0, 4) + "*".repeat(length - 8) + input.substring(length - 4);
return FIELD_IMES+input.substring(0, 8) + "*".repeat(length - 8) + input.substring(length - 8);
}