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) |