mirror of
https://gitee.com/dromara/sa-token.git
synced 2026-02-27 16:50:24 +08:00
NotLoginException 增加新场景值 -6: token 已被冻结
This commit is contained in:
@@ -41,34 +41,38 @@ public class NotLoginException extends SaTokenException {
|
||||
* 因为loginId刚取出的时候类型为String,为了避免两者相比较时不必要的类型转换带来的性能消耗,故在此直接将常量类型设计为String
|
||||
*/
|
||||
|
||||
/** 表示未提供token */
|
||||
/** 表示未能读取到有效 token */
|
||||
public static final String NOT_TOKEN = "-1";
|
||||
public static final String NOT_TOKEN_MESSAGE = "未能读取到有效 token";
|
||||
|
||||
/** 表示token无效 */
|
||||
/** 表示 token 无效 */
|
||||
public static final String INVALID_TOKEN = "-2";
|
||||
public static final String INVALID_TOKEN_MESSAGE = "token 无效";
|
||||
|
||||
/** 表示token已过期 */
|
||||
/** 表示 token 已过期 */
|
||||
public static final String TOKEN_TIMEOUT = "-3";
|
||||
public static final String TOKEN_TIMEOUT_MESSAGE = "token 已过期";
|
||||
|
||||
/** 表示token已被顶下线 */
|
||||
/** 表示 token 已被顶下线 */
|
||||
public static final String BE_REPLACED = "-4";
|
||||
public static final String BE_REPLACED_MESSAGE = "token 已被顶下线";
|
||||
|
||||
/** 表示token已被踢下线 */
|
||||
/** 表示 token 已被踢下线 */
|
||||
public static final String KICK_OUT = "-5";
|
||||
public static final String KICK_OUT_MESSAGE = "token 已被踢下线";
|
||||
|
||||
/** 表示 token 已被冻结 */
|
||||
public static final String TOKEN_FREEZE = "-6";
|
||||
public static final String TOKEN_FREEZE_MESSAGE = "token 已被冻结";
|
||||
|
||||
/** 默认的提示语 */
|
||||
public static final String DEFAULT_MESSAGE = "当前会话未登录";
|
||||
|
||||
|
||||
/**
|
||||
* 代表异常token的标志集合
|
||||
* 代表异常 token 的标志集合
|
||||
*/
|
||||
public static final List<String> ABNORMAL_LIST = Arrays.asList(NOT_TOKEN, INVALID_TOKEN, TOKEN_TIMEOUT, BE_REPLACED, KICK_OUT);
|
||||
public static final List<String> ABNORMAL_LIST = Arrays.asList(NOT_TOKEN, INVALID_TOKEN, TOKEN_TIMEOUT, BE_REPLACED, KICK_OUT, TOKEN_FREEZE);
|
||||
|
||||
|
||||
/**
|
||||
@@ -145,6 +149,9 @@ public class NotLoginException extends SaTokenException {
|
||||
else if(KICK_OUT.equals(type)) {
|
||||
message = KICK_OUT_MESSAGE;
|
||||
}
|
||||
else if(TOKEN_FREEZE.equals(type)) {
|
||||
message = TOKEN_FREEZE_MESSAGE;
|
||||
}
|
||||
else {
|
||||
message = DEFAULT_MESSAGE;
|
||||
}
|
||||
|
||||
@@ -1347,7 +1347,7 @@ public class StpLogic {
|
||||
|
||||
// 3、值为 -2 代表已被冻结,此时需要抛出异常
|
||||
if(timeout == SaTokenDao.NOT_VALUE_EXPIRE) {
|
||||
throw NotLoginException.newInstance(loginType, NotLoginException.TOKEN_TIMEOUT, tokenValue).setCode(SaErrorCode.CODE_11016);
|
||||
throw NotLoginException.newInstance(loginType, NotLoginException.TOKEN_FREEZE, tokenValue).setCode(SaErrorCode.CODE_11016);
|
||||
}
|
||||
// --- 至此,验证已通过
|
||||
|
||||
|
||||
@@ -21,8 +21,7 @@ public class GlobalException {
|
||||
|
||||
// 全局异常拦截(拦截项目中的所有异常)
|
||||
@ExceptionHandler
|
||||
public AjaxJson handlerException(Exception e, HttpServletRequest request, HttpServletResponse response)
|
||||
throws Exception {
|
||||
public AjaxJson handlerException(Exception e, HttpServletRequest request, HttpServletResponse response) {
|
||||
|
||||
// 打印堆栈,以供调试
|
||||
System.out.println("全局异常---------------");
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package com.pj.test;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 登录测试
|
||||
@@ -32,6 +31,13 @@ public class LoginController {
|
||||
return SaResult.ok("是否登录:" + StpUtil.isLogin());
|
||||
}
|
||||
|
||||
// 校验登录 ---- http://localhost:8081/acc/checkLogin
|
||||
@RequestMapping("checkLogin")
|
||||
public SaResult checkLogin() {
|
||||
StpUtil.checkLogin();
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
// 查询 Token 信息 ---- http://localhost:8081/acc/tokenInfo
|
||||
@RequestMapping("tokenInfo")
|
||||
public SaResult tokenInfo() {
|
||||
|
||||
@@ -7,13 +7,14 @@
|
||||
## 何为场景值
|
||||
在前面的章节中,我们了解到,在会话未登录的情况下尝试获取`loginId`会使框架抛出`NotLoginException`异常,而同为未登录异常却有五种抛出场景的区分
|
||||
|
||||
| 场景值 | 对应常量 | 含义说明 |
|
||||
|--- |--- |--- |
|
||||
| -1 | NotLoginException.NOT_TOKEN | 未能从请求中读取到token |
|
||||
| -2 | NotLoginException.INVALID_TOKEN| 已读取到token,但是token无效 |
|
||||
| -3 | NotLoginException.TOKEN_TIMEOUT| 已读取到token,但是token已经过期 |
|
||||
| -4 | NotLoginException.BE_REPLACED| 已读取到token,但是token已被顶下线 |
|
||||
| -5 | NotLoginException.KICK_OUT| 已读取到token,但是token已被踢下线 |
|
||||
| 场景值 | 对应常量 | 含义说明 |
|
||||
|--- |--- |--- |
|
||||
| -1 | NotLoginException.NOT_TOKEN | 未能从请求中读取到有效 token |
|
||||
| -2 | NotLoginException.INVALID_TOKEN | 已读取到 token,但是 token 无效 |
|
||||
| -3 | NotLoginException.TOKEN_TIMEOUT | 已读取到 token,但是 token 已经过期 |
|
||||
| -4 | NotLoginException.BE_REPLACED | 已读取到 token,但是 token 已被顶下线 |
|
||||
| -5 | NotLoginException.KICK_OUT | 已读取到 token,但是 token 已被踢下线 |
|
||||
| -6 | NotLoginException.TOKEN_FREEZE | 已读取到 token,但是 token 已被冻结 |
|
||||
|
||||
|
||||
|
||||
@@ -32,19 +33,22 @@ public SaResult handlerNotLoginException(NotLoginException nle)
|
||||
// 判断场景值,定制化异常信息
|
||||
String message = "";
|
||||
if(nle.getType().equals(NotLoginException.NOT_TOKEN)) {
|
||||
message = "未提供token";
|
||||
message = "未能读取到有效 token";
|
||||
}
|
||||
else if(nle.getType().equals(NotLoginException.INVALID_TOKEN)) {
|
||||
message = "token无效";
|
||||
message = "token 无效";
|
||||
}
|
||||
else if(nle.getType().equals(NotLoginException.TOKEN_TIMEOUT)) {
|
||||
message = "token已过期";
|
||||
message = "token 已过期";
|
||||
}
|
||||
else if(nle.getType().equals(NotLoginException.BE_REPLACED)) {
|
||||
message = "token已被顶下线";
|
||||
message = "token 已被顶下线";
|
||||
}
|
||||
else if(nle.getType().equals(NotLoginException.KICK_OUT)) {
|
||||
message = "token已被踢下线";
|
||||
message = "token 已被踢下线";
|
||||
}
|
||||
else if(nle.getType().equals(NotLoginException.TOKEN_FREEZE)) {
|
||||
message = "token 已被冻结";
|
||||
}
|
||||
else {
|
||||
message = "当前会话未登录";
|
||||
|
||||
Reference in New Issue
Block a user