From cd909f4137f4c6ee2c75ee83ecd4173ad018c7fd Mon Sep 17 00:00:00 2001 From: click33 <2393584716@qq.com> Date: Mon, 8 Aug 2022 16:38:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20`StpUtil.getExtra(tokenVal?= =?UTF-8?q?ue,=20key)`=20=E6=96=B9=E6=B3=95=EF=BC=8C=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E4=BB=BB=E6=84=8F=20token=20=E7=9A=84?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E5=8F=82=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/dev33/satoken/stp/StpLogic.java | 12 +++++++++++- .../src/main/java/cn/dev33/satoken/stp/StpUtil.java | 12 +++++++++++- .../cn/dev33/satoken/jwt/StpLogicJwtForMixin.java | 12 ++++++++++-- .../cn/dev33/satoken/jwt/StpLogicJwtForSimple.java | 12 ++++++++++-- .../dev33/satoken/jwt/StpLogicJwtForStateless.java | 12 ++++++++++-- .../src/test/java/com/pj/test/JwtForMixinTest.java | 7 +++++-- .../src/test/java/com/pj/test/JwtForSimpleTest.java | 6 ++++-- .../test/java/com/pj/test/JwtForStatelessTest.java | 3 +++ 8 files changed, 64 insertions(+), 12 deletions(-) diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java b/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java index 7a0ddb7c..fd323d90 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java @@ -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(); + } // ---- 其它操作 diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpUtil.java b/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpUtil.java index 79984d91..3e24922f 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpUtil.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpUtil.java @@ -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 相关 =================== diff --git a/sa-token-plugin/sa-token-jwt/src/main/java/cn/dev33/satoken/jwt/StpLogicJwtForMixin.java b/sa-token-plugin/sa-token-jwt/src/main/java/cn/dev33/satoken/jwt/StpLogicJwtForMixin.java index b4afc679..8aa555bb 100644 --- a/sa-token-plugin/sa-token-jwt/src/main/java/cn/dev33/satoken/jwt/StpLogicJwtForMixin.java +++ b/sa-token-plugin/sa-token-jwt/src/main/java/cn/dev33/satoken/jwt/StpLogicJwtForMixin.java @@ -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); } /** diff --git a/sa-token-plugin/sa-token-jwt/src/main/java/cn/dev33/satoken/jwt/StpLogicJwtForSimple.java b/sa-token-plugin/sa-token-jwt/src/main/java/cn/dev33/satoken/jwt/StpLogicJwtForSimple.java index ccb3f9b5..6605bb9e 100644 --- a/sa-token-plugin/sa-token-jwt/src/main/java/cn/dev33/satoken/jwt/StpLogicJwtForSimple.java +++ b/sa-token-plugin/sa-token-jwt/src/main/java/cn/dev33/satoken/jwt/StpLogicJwtForSimple.java @@ -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); } } diff --git a/sa-token-plugin/sa-token-jwt/src/main/java/cn/dev33/satoken/jwt/StpLogicJwtForStateless.java b/sa-token-plugin/sa-token-jwt/src/main/java/cn/dev33/satoken/jwt/StpLogicJwtForStateless.java index 43bee718..755d1eca 100644 --- a/sa-token-plugin/sa-token-jwt/src/main/java/cn/dev33/satoken/jwt/StpLogicJwtForStateless.java +++ b/sa-token-plugin/sa-token-jwt/src/main/java/cn/dev33/satoken/jwt/StpLogicJwtForStateless.java @@ -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); } diff --git a/sa-token-test/sa-token-jwt-test/src/test/java/com/pj/test/JwtForMixinTest.java b/sa-token-test/sa-token-jwt-test/src/test/java/com/pj/test/JwtForMixinTest.java index 0c9c3c31..ef6ed9e2 100644 --- a/sa-token-test/sa-token-jwt-test/src/test/java/com/pj/test/JwtForMixinTest.java +++ b/sa-token-test/sa-token-jwt-test/src/test/java/com/pj/test/JwtForMixinTest.java @@ -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); } diff --git a/sa-token-test/sa-token-jwt-test/src/test/java/com/pj/test/JwtForSimpleTest.java b/sa-token-test/sa-token-jwt-test/src/test/java/com/pj/test/JwtForSimpleTest.java index 88c31e2a..0e04ecf8 100644 --- a/sa-token-test/sa-token-jwt-test/src/test/java/com/pj/test/JwtForSimpleTest.java +++ b/sa-token-test/sa-token-jwt-test/src/test/java/com/pj/test/JwtForSimpleTest.java @@ -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); } diff --git a/sa-token-test/sa-token-jwt-test/src/test/java/com/pj/test/JwtForStatelessTest.java b/sa-token-test/sa-token-jwt-test/src/test/java/com/pj/test/JwtForStatelessTest.java index 43a123c7..78793fea 100644 --- a/sa-token-test/sa-token-jwt-test/src/test/java/com/pj/test/JwtForStatelessTest.java +++ b/sa-token-test/sa-token-jwt-test/src/test/java/com/pj/test/JwtForStatelessTest.java @@ -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); }