mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-04-05 08:37:21 +08:00
Pre Merge pull request !327 from Pursuer/dev
This commit is contained in:
commit
4b22a84e7d
@ -0,0 +1,87 @@
|
||||
package cn.dev33.satoken.jwt;
|
||||
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.jwt.error.SaJwtErrorCode;
|
||||
import cn.dev33.satoken.jwt.exception.SaJwtException;
|
||||
import cn.dev33.satoken.stp.StpLogic;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.json.JSONException;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.jwt.JWT;
|
||||
import cn.hutool.jwt.JWTException;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* JWT版本SaToken管理器
|
||||
*
|
||||
* @author binarypursuer
|
||||
* @version 1.0
|
||||
* @date 2025/4/2
|
||||
*/
|
||||
public class JwtSaManager {
|
||||
|
||||
/**
|
||||
* StpLogic 集合, 记录框架所有成功初始化的 StpLogic
|
||||
*/
|
||||
public static Map<String, StpLogic> stpLogicMap = new LinkedHashMap<>();
|
||||
/**
|
||||
* 登录类型
|
||||
*/
|
||||
private static final String LOGIN_TYPE = "loginType";
|
||||
|
||||
/**
|
||||
* 向全局集合中 put 一个 StpLogic
|
||||
*
|
||||
* @param stpLogic StpLogic
|
||||
* @author binarypursuer
|
||||
* @date 2025/4/2
|
||||
* @since 1.0
|
||||
**/
|
||||
public static void putStpLogic(StpLogic stpLogic) {
|
||||
stpLogicMap.put(stpLogic.getLoginType(), stpLogic);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 StpLogic
|
||||
*
|
||||
* @param tokenName Token名称
|
||||
* @return cn.dev33.satoken.stp.StpLogic
|
||||
* @author binarypursuer
|
||||
* @date 2025/4/2
|
||||
* @since 1.0
|
||||
**/
|
||||
public static StpLogic getStpLogic(String tokenName) {
|
||||
// 获取当前请求的token
|
||||
String token = SaHolder.getRequest().getHeader(tokenName);
|
||||
// 判断token是否为空
|
||||
if (token == null) {
|
||||
return null;
|
||||
}
|
||||
// 解析Token
|
||||
JWT jwt;
|
||||
try {
|
||||
jwt = JWT.of(token);
|
||||
} catch (JWTException | JSONException e) {
|
||||
throw new SaJwtException("jwt 解析失败:" + token, e).setCode(SaJwtErrorCode.CODE_30201);
|
||||
}
|
||||
JSONObject payloads = jwt.getPayloads();
|
||||
//获取登录类型
|
||||
String loginType = payloads.getStr(LOGIN_TYPE);
|
||||
// 获取 StpLogic
|
||||
return stpLogicMap.get(loginType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 StpLogic
|
||||
*
|
||||
* @return cn.dev33.satoken.stp.StpLogic
|
||||
* @author binarypursuer
|
||||
* @date 2025/4/2
|
||||
* @since 1.0
|
||||
**/
|
||||
public static StpLogic getStpLogic() {
|
||||
return getStpLogic(StpUtil.getTokenName());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user