优化:jwt模式 token 过期后,抛出的异常描述是 token 已过期,而不再是 token 无效

This commit is contained in:
click33
2024-04-22 02:37:49 +08:00
parent 5e354f3ae2
commit 46771bcb3b
2 changed files with 10 additions and 0 deletions

View File

@@ -21,6 +21,7 @@ import java.util.Map;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.exception.ApiDisabledException;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.jwt.error.SaJwtErrorCode;
import cn.dev33.satoken.jwt.exception.SaJwtException;
import cn.dev33.satoken.stp.SaTokenInfo;
@@ -105,6 +106,10 @@ public class StpLogicJwtForMixin extends StpLogic {
Object loginId = SaJwtUtil.getLoginId(tokenValue, loginType, jwtSecretKey());
return String.valueOf(loginId);
} catch (SaJwtException e) {
// CODE == 30204 时代表token已过期此时返回-3以便外层更精确的显示异常信息
if(e.getCode() == SaJwtErrorCode.CODE_30204) {
return NotLoginException.TOKEN_TIMEOUT;
}
return null;
}
}

View File

@@ -18,6 +18,7 @@ package cn.dev33.satoken.jwt;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.exception.ApiDisabledException;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.jwt.error.SaJwtErrorCode;
import cn.dev33.satoken.jwt.exception.SaJwtException;
import cn.dev33.satoken.listener.SaTokenEventCenter;
@@ -132,6 +133,10 @@ public class StpLogicJwtForStateless extends StpLogic {
Object loginId = SaJwtUtil.getLoginId(tokenValue, loginType, jwtSecretKey());
return String.valueOf(loginId);
} catch (SaJwtException e) {
// CODE == 30204 时代表token已过期此时返回-3以便外层更精确的显示异常信息
if(e.getCode() == SaJwtErrorCode.CODE_30204) {
return NotLoginException.TOKEN_TIMEOUT;
}
return null;
}
}