mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-09-19 10:08:07 +08:00
@@ -4,10 +4,13 @@ import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import cn.dev33.satoken.exception.NotLoginException;
|
||||
import cn.dev33.satoken.exception.SaTokenException;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.jwt.JWT;
|
||||
import cn.hutool.jwt.JWTException;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* jwt操作工具类封装
|
||||
* @author kong
|
||||
@@ -33,7 +36,12 @@ public class SaJwtUtil {
|
||||
/**
|
||||
* key:有效截止期 (时间戳)
|
||||
*/
|
||||
public static final String EFF = "eff";
|
||||
public static final String EFF = "eff";
|
||||
|
||||
/**
|
||||
* key: 扩展数据
|
||||
*/
|
||||
public static final String EXPAND = "expand";
|
||||
|
||||
/**
|
||||
* 当有效期被设为此值时,代表永不过期
|
||||
@@ -97,6 +105,44 @@ public class SaJwtUtil {
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 jwt (全参数方式)
|
||||
* @param loginType 账号类型
|
||||
* @param loginId 账号id
|
||||
* @param device 设备标识
|
||||
* @param expandInfoMap 扩展数据
|
||||
* @param timeout token有效期 (单位 秒)
|
||||
* @param keyt 秘钥
|
||||
* @return jwt-token
|
||||
*/
|
||||
public static String createToken(String loginType, Object loginId, String device,
|
||||
Map<String, Object> expandInfoMap, long timeout, String keyt) {
|
||||
|
||||
// 秘钥不可以为空
|
||||
SaTokenException.throwByNull(keyt, "请配置jwt秘钥");
|
||||
|
||||
// 计算有效期
|
||||
long effTime = timeout;
|
||||
if(timeout != NEVER_EXPIRE) {
|
||||
effTime = timeout * 1000 + System.currentTimeMillis();
|
||||
}
|
||||
|
||||
JWT jwt = JWT.create()
|
||||
.setPayload(LOGIN_TYPE, loginType)
|
||||
.setPayload(LOGIN_ID, loginId)
|
||||
.setPayload(DEVICE, device)
|
||||
.setPayload(EFF, effTime);
|
||||
|
||||
// 设定扩展数据
|
||||
if (CollectionUtil.isNotEmpty(expandInfoMap)) {
|
||||
jwt.setPayload(EXPAND, expandInfoMap);
|
||||
}
|
||||
|
||||
// 返回
|
||||
return jwt.setKey(keyt.getBytes()).sign();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* jwt 解析(校验签名和密码)
|
||||
* @param token Jwt-Token值
|
||||
@@ -188,6 +234,33 @@ public class SaJwtUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* getExpandMap
|
||||
*
|
||||
* @since 2021/11/23 5:05 下午
|
||||
* @param token token值
|
||||
* @param keyt 秘钥
|
||||
* @return 值
|
||||
*/
|
||||
public static Object getExpandInfo(String token, String keyt) {
|
||||
return getPayloads(token, keyt).get(EXPAND);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 jwt 代表的账号id (未登录时返回null)
|
||||
* @param token Token值
|
||||
* @param keyt 秘钥
|
||||
* @return 值
|
||||
*/
|
||||
public static Object getExpandInfoOrNull(String token, String keyt) {
|
||||
try {
|
||||
return getPayloads(token, keyt).get(EXPAND);
|
||||
} catch (NotLoginException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取 jwt 剩余有效期
|
||||
* @param token JwtToken值
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package cn.dev33.satoken.jwt;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
@@ -54,6 +55,11 @@ public class StpLogicJwtForMix extends StpLogic {
|
||||
return SaJwtUtil.createToken(loginType, loginId, device, timeout, jwtSecretKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String createTokenValue(Object loginId, String device, Map<String, Object> expandInfoMap, long timeout) {
|
||||
return SaJwtUtil.createToken(loginType, loginId, device, expandInfoMap, timeout, jwtSecretKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前会话的Token信息
|
||||
* @return token信息
|
||||
@@ -95,6 +101,21 @@ public class StpLogicJwtForMix extends StpLogic {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getExpandInfoNotHandle(String tokenValue) {
|
||||
// 先验证 loginType,如果不符,则视为无效token,返回null
|
||||
String loginType = SaJwtUtil.getPayloadsNotCheck(tokenValue, jwtSecretKey()).getStr(SaJwtUtil.LOGIN_TYPE);
|
||||
if(getLoginType().equals(loginType) == false) {
|
||||
return null;
|
||||
}
|
||||
// 获取 expandInfo
|
||||
try {
|
||||
return SaJwtUtil.getExpandInfo(tokenValue, jwtSecretKey());
|
||||
} catch (NotLoginException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 会话注销
|
||||
*/
|
||||
|
@@ -11,6 +11,8 @@ import cn.dev33.satoken.stp.SaTokenInfo;
|
||||
import cn.dev33.satoken.stp.StpLogic;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Sa-Token 整合 jwt -- stateless 无状态
|
||||
* @author kong
|
||||
@@ -55,6 +57,11 @@ public class StpLogicJwtForStateless extends StpLogic {
|
||||
return SaJwtUtil.createToken(loginType, loginId, device, timeout, jwtSecretKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String createTokenValue(Object loginId, String device, Map<String, Object> expandInfoMap, long timeout) {
|
||||
return SaJwtUtil.createToken(loginType, loginId, device, expandInfoMap, timeout, jwtSecretKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前会话的Token信息
|
||||
* @return token信息
|
||||
@@ -89,7 +96,7 @@ public class StpLogicJwtForStateless extends StpLogic {
|
||||
loginModel.build(getConfig());
|
||||
|
||||
// ------ 2、生成一个token
|
||||
String tokenValue = createTokenValue(id, loginModel.getDeviceOrDefault(), loginModel.getTimeout());
|
||||
String tokenValue = createTokenValue(id, loginModel.getDeviceOrDefault(), loginModel.getExpandInfoMap(), loginModel.getTimeout());
|
||||
|
||||
// 3、在当前会话写入tokenValue
|
||||
setTokenValue(tokenValue, loginModel.getCookieTimeout());
|
||||
@@ -117,6 +124,21 @@ public class StpLogicJwtForStateless extends StpLogic {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getExpandInfoNotHandle(String tokenValue) {
|
||||
// 先验证 loginType,如果不符,则视为无效token,返回null
|
||||
String loginType = SaJwtUtil.getPayloadsNotCheck(tokenValue, jwtSecretKey()).getStr(SaJwtUtil.LOGIN_TYPE);
|
||||
if(getLoginType().equals(loginType) == false) {
|
||||
return null;
|
||||
}
|
||||
// 获取 expandInfoMap
|
||||
try {
|
||||
return SaJwtUtil.getExpandInfo(tokenValue, jwtSecretKey());
|
||||
} catch (NotLoginException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 会话注销
|
||||
*/
|
||||
|
Reference in New Issue
Block a user