From f2e9f7c222a84e9a8e0d9ef7cbb425dd28688b2c Mon Sep 17 00:00:00 2001 From: click33 <2393584716@qq.com> Date: Wed, 2 Apr 2025 05:51:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E9=A1=B9=20rightNowCreateTokenSession=EF=BC=9A=E5=9C=A8?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=97=B6=EF=BC=8C=E6=98=AF=E5=90=A6=E7=AB=8B?= =?UTF-8?q?=E5=8D=B3=E5=88=9B=E5=BB=BA=E5=AF=B9=E5=BA=94=E7=9A=84=20Token-?= =?UTF-8?q?Session=20=EF=BC=88true=3D=E5=9C=A8=E7=99=BB=E5=BD=95=E6=97=B6?= =?UTF-8?q?=E7=AB=8B=E5=8D=B3=E5=88=9B=E5=BB=BA=EF=BC=8Cfalse=3D=E5=9C=A8?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E8=B0=83=E7=94=A8=20getTokenSession?= =?UTF-8?q?()=20=E6=97=B6=E5=88=9B=E5=BB=BA=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev33/satoken/config/SaTokenConfig.java | 26 ++++++++++++++++++ .../java/cn/dev33/satoken/stp/StpLogic.java | 2 +- .../stp/parameter/SaLoginParameter.java | 27 +++++++++++++++++++ sa-token-doc/up/login-parameter.md | 1 + sa-token-doc/use/config.md | 1 + 5 files changed, 56 insertions(+), 1 deletion(-) diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/config/SaTokenConfig.java b/sa-token-core/src/main/java/cn/dev33/satoken/config/SaTokenConfig.java index 4d375572..96af30b4 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/config/SaTokenConfig.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/config/SaTokenConfig.java @@ -128,6 +128,11 @@ public class SaTokenConfig implements Serializable { */ private Boolean isLogoutKeepTokenSession = false; + /** + * 在登录时,是否立即创建对应的 Token-Session (true=在登录时立即创建,false=在第一次调用 getTokenSession() 时创建) + */ + private Boolean rightNowCreateTokenSession = false; + /** * token 风格(默认可取值:uuid、simple-uuid、random-32、random-64、random-128、tik) */ @@ -821,6 +826,26 @@ public class SaTokenConfig implements Serializable { return this; } + /** + * 获取 在登录时,是否立即创建对应的 Token-Session (true=在登录时立即创建,false=在第一次调用 getTokenSession() 时创建) + * + * @return / + */ + public Boolean getRightNowCreateTokenSession() { + return this.rightNowCreateTokenSession; + } + + /** + * 设置 在登录时,是否立即创建对应的 Token-Session (true=在登录时立即创建,false=在第一次调用 getTokenSession() 时创建) + * + * @param rightNowCreateTokenSession / + * @return 对象自身 + */ + public SaTokenConfig setRightNowCreateTokenSession(Boolean rightNowCreateTokenSession) { + this.rightNowCreateTokenSession = rightNowCreateTokenSession; + return this; + } + /** * @return Cookie 全局配置对象 */ @@ -895,6 +920,7 @@ public class SaTokenConfig implements Serializable { + ", logoutRange=" + logoutRange + ", isLogoutKeepFreezeOps=" + isLogoutKeepFreezeOps + ", isLogoutKeepTokenSession=" + isLogoutKeepTokenSession + + ", rightNowCreateTokenSession=" + rightNowCreateTokenSession + ", tokenStyle=" + tokenStyle + ", dataRefreshPeriod=" + dataRefreshPeriod + ", tokenSessionCheckLogin=" + tokenSessionCheckLogin 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 9b647f4b..09a38660 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 @@ -498,7 +498,7 @@ public class StpLogic { } // 7、如果该 token 对应的 Token-Session 已经存在,则需要给其续期 - SaSession tokenSession = getTokenSessionByToken(tokenValue, false); + SaSession tokenSession = getTokenSessionByToken(tokenValue, loginParameter.getRightNowCreateTokenSession()); if(tokenSession != null) { tokenSession.updateMinTimeout(loginParameter.getTimeout()); } diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/stp/parameter/SaLoginParameter.java b/sa-token-core/src/main/java/cn/dev33/satoken/stp/parameter/SaLoginParameter.java index 476ff8c2..d6688430 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/stp/parameter/SaLoginParameter.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/stp/parameter/SaLoginParameter.java @@ -118,6 +118,11 @@ public class SaLoginParameter { */ private SaLogoutMode overflowLogoutMode; + /** + * 在登录时,是否立即创建对应的 Token-Session (true=在登录时立即创建,false=在第一次调用 getTokenSession() 时创建) + */ + private Boolean rightNowCreateTokenSession; + // ------ 附加方法 @@ -145,6 +150,7 @@ public class SaLoginParameter { this.isWriteHeader = config.getIsWriteHeader(); this.replacedRange = config.getReplacedRange(); this.overflowLogoutMode = config.getOverflowLogoutMode(); + this.rightNowCreateTokenSession = config.getRightNowCreateTokenSession(); return this; } @@ -498,6 +504,26 @@ public class SaLoginParameter { return this; } + /** + * 获取 在登录时,是否立即创建对应的 Token-Session (true=在登录时立即创建,false=在第一次调用 getTokenSession() 时创建) + * + * @return / + */ + public Boolean getRightNowCreateTokenSession() { + return this.rightNowCreateTokenSession; + } + + /** + * 设置 在登录时,是否立即创建对应的 Token-Session (true=在登录时立即创建,false=在第一次调用 getTokenSession() 时创建) + * + * @param rightNowCreateTokenSession / + * @return 对象自身 + */ + public SaLoginParameter setRightNowCreateTokenSession(Boolean rightNowCreateTokenSession) { + this.rightNowCreateTokenSession = rightNowCreateTokenSession; + return this; + } + /* * toString */ @@ -519,6 +545,7 @@ public class SaLoginParameter { + ", token=" + token + ", isWriteHeader=" + isWriteHeader + ", terminalTag=" + terminalExtraData + + ", rightNowCreateTokenSession=" + rightNowCreateTokenSession + "]"; } diff --git a/sa-token-doc/up/login-parameter.md b/sa-token-doc/up/login-parameter.md index 06c9b806..b122e4f9 100644 --- a/sa-token-doc/up/login-parameter.md +++ b/sa-token-doc/up/login-parameter.md @@ -31,6 +31,7 @@ StpUtil.login(10001, new SaLoginParameter() .setTerminalExtra("key", "value")// 本次登录挂载到 SaTerminalInfo 的自定义扩展数据 .setReplacedRange(SaReplacedRange.CURR_DEVICE_TYPE) // 顶人下线的范围: CURR_DEVICE_TYPE=当前指定的设备类型端, ALL_DEVICE_TYPE=所有设备类型端 .setOverflowLogoutMode(SaLogoutMode.LOGOUT) // 溢出 maxLoginCount 的客户端,将以何种方式注销下线: LOGOUT=注销下线, KICKOUT=踢人下线, REPLACED=顶人下线 + .setRightNowCreateTokenSession(true) // 是否立即创建对应的 Token-Session (true=在登录时立即创建,false=在第一次调用 getTokenSession() 时创建) ); ``` diff --git a/sa-token-doc/use/config.md b/sa-token-doc/use/config.md index 70f888f1..e4c20195 100644 --- a/sa-token-doc/use/config.md +++ b/sa-token-doc/use/config.md @@ -134,6 +134,7 @@ public class SaTokenConfigure { | logoutRange | SaLogoutRange | TOKEN | 注销范围 (TOKEN=只注销当前 token 的会话,ACCOUNT=注销当前 token 指向的 loginId 其所有客户端会话) (此参数只在调用 StpUtil.logout() 时有效) | | isLogoutKeepFreezeOps | Boolean | false | 如果 token 已被冻结,是否保留其操作权 (是否允许此 token 调用注销API) (此参数只在调用 StpUtil.[logout/kickout/replaced]ByTokenValue("token") 时有效) | | isLogoutKeepTokenSession | Boolean | false | 在注销 token 后,是否保留其对应的 Token-Session | +| rightNowCreateTokenSession| Boolean | false | 在登录时,是否立即创建对应的 Token-Session (true=在登录时立即创建,false=在第一次调用 getTokenSession() 时创建) | | tokenStyle | String | uuid | token风格, [参考:自定义Token风格](/up/token-style) | | dataRefreshPeriod | int | 30 | 默认数据持久组件实现类中,每次清理过期数据间隔的时间 (单位: 秒) ,默认值30秒,设置为-1代表不启动定时清理 | | tokenSessionCheckLogin | Boolean | true | 获取 `Token-Session` 时是否必须登录 (如果配置为true,会在每次获取 `Token-Session` 时校验是否登录),[详解](/use/config?id=配置项详解:tokenSessionCheckLogin) |