diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/stp/SaLoginParameter.java b/sa-token-core/src/main/java/cn/dev33/satoken/stp/SaLoginParameter.java index 59d24882..947708aa 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/stp/SaLoginParameter.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/stp/SaLoginParameter.java @@ -84,12 +84,17 @@ public class SaLoginParameter { /** * 在多人登录同一账号时,是否共用一个 token (为 true 时所有登录共用一个 token, 为 false 时每次登录新建一个 token) */ - private Boolean isShare = true; + private Boolean isShare; /** * 同一账号最大登录数量,-1代表不限 (只有在 isConcurrent=true, isShare=false 时此配置项才有意义) */ - private int maxLoginCount = 12; + private int maxLoginCount; + + /** + * 在每次创建 token 时的最高循环次数,用于保证 token 唯一性(-1=不循环尝试,直接使用) + */ + private int maxTryTimes; /** * 是否为持久Cookie(临时Cookie在浏览器关闭时会自动删除,持久Cookie在重新打开后依然存在) @@ -122,6 +127,8 @@ public class SaLoginParameter { this.timeout = config.getTimeout(); this.isConcurrent = config.getIsConcurrent(); this.isShare = config.getIsShare(); + this.maxLoginCount = config.getMaxLoginCount(); + this.maxTryTimes = config.getMaxTryTimes(); this.isLastingCookie = config.getIsLastingCookie(); this.isWriteHeader = config.getIsWriteHeader(); return this; @@ -314,6 +321,22 @@ public class SaLoginParameter { return this; } + /** + * @return 在每次创建 token 时的最高循环次数,用于保证 token 唯一性(-1=不循环尝试,直接使用) + */ + public int getMaxTryTimes() { + return maxTryTimes; + } + + /** + * @param maxTryTimes 在每次创建 token 时的最高循环次数,用于保证 token 唯一性(-1=不循环尝试,直接使用) + * @return 对象自身 + */ + public SaLoginParameter setMaxTryTimes(int maxTryTimes) { + this.maxTryTimes = maxTryTimes; + return this; + } + /** * @return 扩展信息(只在jwt模式下生效) */ @@ -395,6 +418,7 @@ public class SaLoginParameter { + ", isConcurrent=" + isConcurrent + ", isShare=" + isShare + ", maxLoginCount=" + maxLoginCount + + ", maxTryTimes=" + maxTryTimes + ", extraData=" + extraData + ", token=" + token + ", isWriteHeader=" + isWriteHeader 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 4c620d6a..1ea874e8 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 @@ -547,7 +547,7 @@ public class StpLogic { // 4、如果代码走到此处,说明未能成功复用旧 token,需要根据算法新建 token return SaStrategy.instance.generateUniqueToken.execute( "token", - getConfigOfMaxTryTimes(), + loginParameter.getMaxTryTimes(), () -> { return createTokenValue(id, loginParameter.getDevice(), loginParameter.getTimeout(), loginParameter.getExtraData()); }, diff --git a/sa-token-demo/sa-token-demo-test/src/main/java/com/pj/test/TestController.java b/sa-token-demo/sa-token-demo-test/src/main/java/com/pj/test/TestController.java index 822e08ce..dc5758c0 100644 --- a/sa-token-demo/sa-token-demo-test/src/main/java/com/pj/test/TestController.java +++ b/sa-token-demo/sa-token-demo-test/src/main/java/com/pj/test/TestController.java @@ -30,6 +30,7 @@ public class TestController { .setIsConcurrent(true) .setIsShare(false) .setMaxLoginCount(4) + .setMaxTryTimes(12) ); return SaResult.ok("登录成功"); }