mirror of
https://gitee.com/dromara/sa-token.git
synced 2026-02-27 16:50:24 +08:00
重构异常状态码机制
This commit is contained in:
@@ -6,7 +6,8 @@ import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import cn.dev33.satoken.exception.SaTokenException;
|
||||
import cn.dev33.satoken.sso.error.SaSsoErrorCode;
|
||||
import cn.dev33.satoken.sso.exception.SaSsoException;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
|
||||
@@ -385,7 +386,7 @@ public class SaSsoConfig implements Serializable {
|
||||
* SSO-Client端:发送Http请求的处理函数
|
||||
*/
|
||||
public Function<String, String> sendHttp = url -> {
|
||||
throw new SaTokenException("请配置 Http 请求处理器");
|
||||
throw new SaSsoException("请配置 Http 请求处理器").setCode(SaSsoErrorCode.CODE_30010);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@ import cn.dev33.satoken.config.SaSsoConfig;
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.context.model.SaRequest;
|
||||
import cn.dev33.satoken.context.model.SaResponse;
|
||||
import cn.dev33.satoken.sso.error.SaSsoErrorCode;
|
||||
import cn.dev33.satoken.sso.exception.SaSsoException;
|
||||
import cn.dev33.satoken.sso.exception.SaSsoExceptionCode;
|
||||
import cn.dev33.satoken.sso.name.ApiName;
|
||||
import cn.dev33.satoken.sso.name.ParamName;
|
||||
import cn.dev33.satoken.stp.StpLogic;
|
||||
@@ -257,7 +257,7 @@ public class SaSsoHandle {
|
||||
|
||||
// ------- 2、如果 loginId 无值,说明 ticket 无效
|
||||
if(SaFoxUtil.isEmpty(loginId)) {
|
||||
throw new SaSsoException("无效ticket:" + ticket).setCode(SaSsoExceptionCode.CODE_20004);
|
||||
throw new SaSsoException("无效ticket:" + ticket).setCode(SaSsoErrorCode.CODE_30004);
|
||||
} else {
|
||||
// 3、如果 loginId 有值,说明 ticket 有效,此时进行登录并重定向至back地址
|
||||
stpLogic.login(loginId);
|
||||
@@ -311,7 +311,7 @@ public class SaSsoHandle {
|
||||
return ssoLogoutBack(req, res);
|
||||
} else {
|
||||
// 将 sso-server 回应的消息作为异常抛出
|
||||
throw new SaSsoException(result.getMsg()).setCode(SaSsoExceptionCode.CODE_20006);
|
||||
throw new SaSsoException(result.getMsg()).setCode(SaSsoErrorCode.CODE_30006);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -400,7 +400,7 @@ public class SaSsoHandle {
|
||||
return result.getData();
|
||||
} else {
|
||||
// 将 sso-server 回应的消息作为异常抛出
|
||||
throw new SaSsoException(result.getMsg()).setCode(SaSsoExceptionCode.CODE_20005);
|
||||
throw new SaSsoException(result.getMsg()).setCode(SaSsoErrorCode.CODE_30005);
|
||||
}
|
||||
} else {
|
||||
// q2、使用模式二:直连Redis校验ticket
|
||||
|
||||
@@ -4,8 +4,8 @@ import cn.dev33.satoken.config.SaSsoConfig;
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.context.model.SaRequest;
|
||||
import cn.dev33.satoken.context.model.SaResponse;
|
||||
import cn.dev33.satoken.sso.error.SaSsoErrorCode;
|
||||
import cn.dev33.satoken.sso.exception.SaSsoException;
|
||||
import cn.dev33.satoken.sso.exception.SaSsoExceptionCode;
|
||||
import cn.dev33.satoken.sso.name.ApiName;
|
||||
import cn.dev33.satoken.sso.name.ParamName;
|
||||
import cn.dev33.satoken.stp.StpLogic;
|
||||
@@ -276,7 +276,7 @@ public class SaSsoProcessor {
|
||||
|
||||
// ------- 2、如果 loginId 无值,说明 ticket 无效
|
||||
if(SaFoxUtil.isEmpty(loginId)) {
|
||||
throw new SaSsoException("无效ticket:" + ticket).setCode(SaSsoExceptionCode.CODE_20004);
|
||||
throw new SaSsoException("无效ticket:" + ticket).setCode(SaSsoErrorCode.CODE_30004);
|
||||
} else {
|
||||
// 3、如果 loginId 有值,说明 ticket 有效,此时进行登录并重定向至back地址
|
||||
stpLogic.login(loginId);
|
||||
@@ -352,7 +352,7 @@ public class SaSsoProcessor {
|
||||
return ssoLogoutBack(req, res);
|
||||
} else {
|
||||
// 将 sso-server 回应的消息作为异常抛出
|
||||
throw new SaSsoException(result.getMsg()).setCode(SaSsoExceptionCode.CODE_20006);
|
||||
throw new SaSsoException(result.getMsg()).setCode(SaSsoErrorCode.CODE_30006);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -446,7 +446,7 @@ public class SaSsoProcessor {
|
||||
return result.getData();
|
||||
} else {
|
||||
// 将 sso-server 回应的消息作为异常抛出
|
||||
throw new SaSsoException(result.getMsg()).setCode(SaSsoExceptionCode.CODE_20005);
|
||||
throw new SaSsoException(result.getMsg()).setCode(SaSsoErrorCode.CODE_30005);
|
||||
}
|
||||
} else {
|
||||
// q2、使用模式二:直连Redis校验ticket
|
||||
|
||||
@@ -11,8 +11,8 @@ import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.config.SaSsoConfig;
|
||||
import cn.dev33.satoken.context.model.SaRequest;
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
import cn.dev33.satoken.sso.error.SaSsoErrorCode;
|
||||
import cn.dev33.satoken.sso.exception.SaSsoException;
|
||||
import cn.dev33.satoken.sso.exception.SaSsoExceptionCode;
|
||||
import cn.dev33.satoken.sso.name.ApiName;
|
||||
import cn.dev33.satoken.sso.name.ParamName;
|
||||
import cn.dev33.satoken.stp.StpLogic;
|
||||
@@ -211,7 +211,7 @@ public class SaSsoTemplate {
|
||||
|
||||
// 1、是否是一个有效的url
|
||||
if(SaFoxUtil.isUrl(url) == false) {
|
||||
throw new SaSsoException("无效redirect:" + url).setCode(SaSsoExceptionCode.CODE_20001);
|
||||
throw new SaSsoException("无效redirect:" + url).setCode(SaSsoErrorCode.CODE_30001);
|
||||
}
|
||||
|
||||
// 2、截取掉?后面的部分
|
||||
@@ -223,7 +223,7 @@ public class SaSsoTemplate {
|
||||
// 3、是否在[允许地址列表]之中
|
||||
List<String> authUrlList = Arrays.asList(getAllowUrl().replaceAll(" ", "").split(","));
|
||||
if(SaStrategy.me.hasElement.apply(authUrlList, url) == false) {
|
||||
throw new SaSsoException("非法redirect:" + url).setCode(SaSsoExceptionCode.CODE_20002);
|
||||
throw new SaSsoException("非法redirect:" + url).setCode(SaSsoErrorCode.CODE_30002);
|
||||
}
|
||||
|
||||
// 校验通过 √
|
||||
@@ -448,7 +448,7 @@ public class SaSsoTemplate {
|
||||
// 默认从配置文件中返回
|
||||
String secretkey = SaSsoManager.getConfig().getSecretkey();
|
||||
if(SaFoxUtil.isEmpty(secretkey)) {
|
||||
throw new SaSsoException("请配置 secretkey 参数").setCode(SaSsoExceptionCode.CODE_20009);
|
||||
throw new SaSsoException("请配置 secretkey 参数").setCode(SaSsoErrorCode.CODE_30009);
|
||||
}
|
||||
return secretkey;
|
||||
}
|
||||
@@ -460,7 +460,7 @@ public class SaSsoTemplate {
|
||||
@Deprecated
|
||||
public void checkSecretkey(String secretkey) {
|
||||
if(SaFoxUtil.isEmpty(secretkey) || secretkey.equals(getSecretkey()) == false) {
|
||||
throw new SaSsoException("无效秘钥:" + secretkey).setCode(SaSsoExceptionCode.CODE_20003);
|
||||
throw new SaSsoException("无效秘钥:" + secretkey).setCode(SaSsoErrorCode.CODE_30003);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -519,7 +519,7 @@ public class SaSsoTemplate {
|
||||
// 校验签名
|
||||
String calcSign = getSign(loginId, timestamp, nonce, getSecretkey());
|
||||
if(calcSign.equals(sign) == false) {
|
||||
throw new SaSsoException("签名无效:" + calcSign).setCode(SaSsoExceptionCode.CODE_20008);
|
||||
throw new SaSsoException("签名无效:" + calcSign).setCode(SaSsoErrorCode.CODE_30008);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -531,7 +531,7 @@ public class SaSsoTemplate {
|
||||
long disparity = Math.abs(System.currentTimeMillis() - timestamp);
|
||||
long allowDisparity = SaSsoManager.getConfig().getTimestampDisparity();
|
||||
if(allowDisparity != -1 && disparity > allowDisparity) {
|
||||
throw new SaSsoException("timestamp 超出允许的范围").setCode(SaSsoExceptionCode.CODE_20007);
|
||||
throw new SaSsoException("timestamp 超出允许的范围").setCode(SaSsoErrorCode.CODE_30007);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,38 +1,41 @@
|
||||
package cn.dev33.satoken.sso.exception;
|
||||
package cn.dev33.satoken.sso.error;
|
||||
|
||||
/**
|
||||
* 定义所有 SSO 异常细分状态码
|
||||
* 定义 sa-token-sso 所有异常细分状态码
|
||||
*
|
||||
* @author kong
|
||||
* @since: 2022-4-25
|
||||
* @since: 2022-10-31
|
||||
*/
|
||||
public class SaSsoExceptionCode {
|
||||
public interface SaSsoErrorCode {
|
||||
|
||||
/** redirect 重定向 url 是一个无效地址 */
|
||||
public static final int CODE_20001 = 20001;
|
||||
public static final int CODE_30001 = 30001;
|
||||
|
||||
/** redirect 重定向 url 不在 allowUrl 允许的范围内 */
|
||||
public static final int CODE_20002 = 20002;
|
||||
public static final int CODE_30002 = 30002;
|
||||
|
||||
/** 接口调用方提供的 secretkey 秘钥无效 */
|
||||
public static final int CODE_20003 = 20003;
|
||||
public static final int CODE_30003 = 30003;
|
||||
|
||||
/** 提供的 ticket 是无效的 */
|
||||
public static final int CODE_20004 = 20004;
|
||||
public static final int CODE_30004 = 30004;
|
||||
|
||||
/** 在模式三下,sso-client 调用 sso-server 端 校验ticket接口 时,得到的响应是校验失败 */
|
||||
public static final int CODE_20005 = 20005;
|
||||
public static final int CODE_30005 = 30005;
|
||||
|
||||
/** 在模式三下,sso-client 调用 sso-server 端 单点注销接口 时,得到的响应是注销失败 */
|
||||
public static final int CODE_20006 = 20006;
|
||||
public static final int CODE_30006 = 30006;
|
||||
|
||||
/** http 请求调用 提供的 timestamp 与当前时间的差距超出允许的范围 */
|
||||
public static final int CODE_20007 = 20007;
|
||||
public static final int CODE_30007 = 30007;
|
||||
|
||||
/** http 请求调用 提供的 sign 无效 */
|
||||
public static final int CODE_20008 = 20008;
|
||||
public static final int CODE_30008 = 30008;
|
||||
|
||||
/** 本地系统没有配置 secretkey 字段 */
|
||||
public static final int CODE_20009 = 20009;
|
||||
public static final int CODE_30009 = 30009;
|
||||
|
||||
/** 本地系统没有配置 http 请求处理器 */
|
||||
public static final int CODE_30010 = 30010;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user