mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-04-05 17:37:53 +08:00
新增 StpUtil.getExtra(tokenValue, key)
方法,用于获取任意 token 的扩展参数。
This commit is contained in:
parent
a2ec360aef
commit
cd909f4137
@ -743,13 +743,23 @@ public class StpLogic {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Token扩展信息(只在jwt模式下有效)
|
||||
* 获取当前 Token 的扩展信息(此函数只在jwt模式下生效)
|
||||
* @param key 键值
|
||||
* @return 对应的扩展数据
|
||||
*/
|
||||
public Object getExtra(String key) {
|
||||
throw new ApiDisabledException();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定 Token 的扩展信息(此函数只在jwt模式下生效)
|
||||
* @param tokenValue 指定的 Token 值
|
||||
* @param key 键值
|
||||
* @return 对应的扩展数据
|
||||
*/
|
||||
public Object getExtra(String tokenValue, String key) {
|
||||
throw new ApiDisabledException();
|
||||
}
|
||||
|
||||
|
||||
// ---- 其它操作
|
||||
|
@ -313,13 +313,23 @@ public class StpUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Token扩展信息(只在jwt模式下有效)
|
||||
* 获取当前 Token 的扩展信息(此函数只在jwt模式下生效)
|
||||
* @param key 键值
|
||||
* @return 对应的扩展数据
|
||||
*/
|
||||
public static Object getExtra(String key) {
|
||||
return stpLogic.getExtra(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定 Token 的扩展信息(此函数只在jwt模式下生效)
|
||||
* @param tokenValue 指定的 Token 值
|
||||
* @param key 键值
|
||||
* @return 对应的扩展数据
|
||||
*/
|
||||
public static Object getExtra(String tokenValue, String key) {
|
||||
return stpLogic.getExtra(tokenValue, key);
|
||||
}
|
||||
|
||||
|
||||
// =================== User-Session 相关 ===================
|
||||
|
@ -150,11 +150,19 @@ public class StpLogicJwtForMixin extends StpLogic {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Token携带的扩展信息
|
||||
* 获取当前 Token 的扩展信息
|
||||
*/
|
||||
@Override
|
||||
public Object getExtra(String key) {
|
||||
return SaJwtUtil.getPayloads(getTokenValue(), loginType, jwtSecretKey()).get(key);
|
||||
return getExtra(getTokenValue(), key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定 Token 的扩展信息
|
||||
*/
|
||||
@Override
|
||||
public Object getExtra(String tokenValue, String key) {
|
||||
return SaJwtUtil.getPayloads(tokenValue, loginType, jwtSecretKey()).get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -49,11 +49,19 @@ public class StpLogicJwtForSimple extends StpLogic {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Token携带的扩展信息
|
||||
* 获取当前 Token 的扩展信息
|
||||
*/
|
||||
@Override
|
||||
public Object getExtra(String key) {
|
||||
return SaJwtUtil.getPayloadsNotCheck(getTokenValue(), loginType, jwtSecretKey()).get(key);
|
||||
return getExtra(getTokenValue(), key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定 Token 的扩展信息
|
||||
*/
|
||||
@Override
|
||||
public Object getExtra(String tokenValue, String key) {
|
||||
return SaJwtUtil.getPayloadsNotCheck(tokenValue, loginType, jwtSecretKey()).get(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -138,11 +138,19 @@ public class StpLogicJwtForStateless extends StpLogic {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Token携带的扩展信息
|
||||
* 获取当前 Token 的扩展信息
|
||||
*/
|
||||
@Override
|
||||
public Object getExtra(String key) {
|
||||
return SaJwtUtil.getPayloads(getTokenValue(), loginType, jwtSecretKey()).get(key);
|
||||
return getExtra(getTokenValue(), key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定 Token 的扩展信息
|
||||
*/
|
||||
@Override
|
||||
public Object getExtra(String tokenValue, String key) {
|
||||
return SaJwtUtil.getPayloads(tokenValue, loginType, jwtSecretKey()).get(key);
|
||||
}
|
||||
|
||||
|
||||
|
@ -38,14 +38,14 @@ public class JwtForMixinTest {
|
||||
// 开始
|
||||
@BeforeAll
|
||||
public static void beforeClass() {
|
||||
System.out.println("\n\n------------------------ JwtForMixTest star ...");
|
||||
System.out.println("\n\n------------------------ JwtForMixinTest star ...");
|
||||
StpUtil.setStpLogic(new StpLogicJwtForMixin());
|
||||
}
|
||||
|
||||
// 结束
|
||||
@AfterAll
|
||||
public static void afterClass() {
|
||||
System.out.println("\n\n------------------------ JwtForMixTest end ... \n");
|
||||
System.out.println("\n\n------------------------ JwtForMixinTest end ... \n");
|
||||
}
|
||||
|
||||
// 测试:登录
|
||||
@ -261,9 +261,12 @@ public class JwtForMixinTest {
|
||||
public void getExtra() {
|
||||
// 登录
|
||||
StpUtil.login(10001, SaLoginConfig.setExtra("name", "zhangsan"));
|
||||
String tokenValue = StpUtil.getTokenValue();
|
||||
|
||||
// 可以取到
|
||||
Assertions.assertEquals(StpUtil.getExtra("name"), "zhangsan");
|
||||
Assertions.assertEquals(StpUtil.getExtra(tokenValue, "name"), "zhangsan");
|
||||
|
||||
// 取不到
|
||||
Assertions.assertEquals(StpUtil.getExtra("name2"), null);
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ public class JwtForSimpleTest {
|
||||
// 开始
|
||||
@BeforeAll
|
||||
public static void beforeClass() {
|
||||
System.out.println("\n\n------------------------ JwtForStyleTest star ...");
|
||||
System.out.println("\n\n------------------------ JwtForSimpleTest star ...");
|
||||
dao = SaManager.getSaTokenDao();
|
||||
StpUtil.setStpLogic(new StpLogicJwtForSimple());
|
||||
}
|
||||
@ -40,7 +40,7 @@ public class JwtForSimpleTest {
|
||||
// 结束
|
||||
@AfterAll
|
||||
public static void afterClass() {
|
||||
System.out.println("\n\n------------------------ JwtForStyleTest end ... \n");
|
||||
System.out.println("\n\n------------------------ JwtForSimpleTest end ... \n");
|
||||
}
|
||||
|
||||
// 测试:登录
|
||||
@ -76,9 +76,11 @@ public class JwtForSimpleTest {
|
||||
public void getExtra() {
|
||||
// 登录
|
||||
StpUtil.login(10001, SaLoginConfig.setExtra("name", "zhangsan"));
|
||||
String tokenValue = StpUtil.getTokenValue();
|
||||
|
||||
// 可以取到
|
||||
Assertions.assertEquals(StpUtil.getExtra("name"), "zhangsan");
|
||||
Assertions.assertEquals(StpUtil.getExtra(tokenValue, "name"), "zhangsan");
|
||||
// 取不到
|
||||
Assertions.assertEquals(StpUtil.getExtra("name2"), null);
|
||||
}
|
||||
|
@ -170,9 +170,12 @@ public class JwtForStatelessTest {
|
||||
public void getExtra() {
|
||||
// 登录
|
||||
StpUtil.login(10001, SaLoginConfig.setExtra("name", "zhangsan"));
|
||||
String tokenValue = StpUtil.getTokenValue();
|
||||
|
||||
// 可以取到
|
||||
Assertions.assertEquals(StpUtil.getExtra("name"), "zhangsan");
|
||||
Assertions.assertEquals(StpUtil.getExtra(tokenValue, "name"), "zhangsan");
|
||||
|
||||
// 取不到
|
||||
Assertions.assertEquals(StpUtil.getExtra("name2"), null);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user