feat: SaLoginParameter 支持 maxLoginCount 配置

This commit is contained in:
click33 2025-03-02 01:52:25 +08:00
parent 3bc9e88645
commit 0b85c7d094
3 changed files with 30 additions and 7 deletions

View File

@ -46,17 +46,17 @@ public class SaLoginParameter {
/** /**
* 此次登录的客户端设备类型 * 此次登录的客户端设备类型
*/ */
public String device; private String device;
/** /**
* 扩展信息只在 jwt 模式下生效 * 扩展信息只在 jwt 模式下生效
*/ */
public Map<String, Object> extraData; private Map<String, Object> extraData;
/** /**
* 预定Token预定本次登录生成的Token值 * 预定Token预定本次登录生成的Token值
*/ */
public String token; private String token;
/** /**
* 本次登录挂载到 TokenSign 的数据 * 本次登录挂载到 TokenSign 的数据
@ -69,7 +69,7 @@ public class SaLoginParameter {
/** /**
* 指定此次登录 token 有效期单位 如未指定自动取全局配置的 timeout * 指定此次登录 token 有效期单位 如未指定自动取全局配置的 timeout
*/ */
public Long timeout; private Long timeout;
/** /**
* 指定此次登录 token 最低活跃频率单位如未指定则使用全局配置的 activeTimeout * 指定此次登录 token 最低活跃频率单位如未指定则使用全局配置的 activeTimeout
@ -86,10 +86,15 @@ public class SaLoginParameter {
*/ */
private Boolean isShare = true; private Boolean isShare = true;
/**
* 同一账号最大登录数量-1代表不限 只有在 isConcurrent=true, isShare=false 时此配置项才有意义
*/
private int maxLoginCount = 12;
/** /**
* 是否为持久Cookie临时Cookie在浏览器关闭时会自动删除持久Cookie在重新打开后依然存在 * 是否为持久Cookie临时Cookie在浏览器关闭时会自动删除持久Cookie在重新打开后依然存在
*/ */
public Boolean isLastingCookie; private Boolean isLastingCookie;
/** /**
* 是否在登录后将 Token 写入到响应头 * 是否在登录后将 Token 写入到响应头
@ -293,6 +298,22 @@ public class SaLoginParameter {
return this; return this;
} }
/**
* @return 同一账号最大登录数量-1代表不限 只有在 isConcurrent=true, isShare=false 时此配置项才有意义
*/
public int getMaxLoginCount() {
return maxLoginCount;
}
/**
* @param maxLoginCount 同一账号最大登录数量-1代表不限 只有在 isConcurrent=true, isShare=false 时此配置项才有意义
* @return 对象自身
*/
public SaLoginParameter setMaxLoginCount(int maxLoginCount) {
this.maxLoginCount = maxLoginCount;
return this;
}
/** /**
* @return 扩展信息只在jwt模式下生效 * @return 扩展信息只在jwt模式下生效
*/ */
@ -373,6 +394,7 @@ public class SaLoginParameter {
+ ", activeTimeout=" + activeTimeout + ", activeTimeout=" + activeTimeout
+ ", isConcurrent=" + isConcurrent + ", isConcurrent=" + isConcurrent
+ ", isShare=" + isShare + ", isShare=" + isShare
+ ", maxLoginCount=" + maxLoginCount
+ ", extraData=" + extraData + ", extraData=" + extraData
+ ", token=" + token + ", token=" + token
+ ", isWriteHeader=" + isWriteHeader + ", isWriteHeader=" + isWriteHeader

View File

@ -496,8 +496,8 @@ public class StpLogic {
SaTokenEventCenter.doLogin(loginType, id, tokenValue, loginParameter); SaTokenEventCenter.doLogin(loginType, id, tokenValue, loginParameter);
// 9检查此账号会话数量是否超出最大值如果超过则按照登录时间顺序把最开始登录的给注销掉 // 9检查此账号会话数量是否超出最大值如果超过则按照登录时间顺序把最开始登录的给注销掉
if(config.getMaxLoginCount() != -1) { if(loginParameter.getMaxLoginCount() != -1) {
logoutByMaxLoginCount(id, session, null, config.getMaxLoginCount()); logoutByMaxLoginCount(id, session, null, loginParameter.getMaxLoginCount());
} }
// 10一切处理完毕返回会话凭证 token // 10一切处理完毕返回会话凭证 token

View File

@ -29,6 +29,7 @@ public class TestController {
StpUtil.login(id, new SaLoginParameter() StpUtil.login(id, new SaLoginParameter()
.setIsConcurrent(true) .setIsConcurrent(true)
.setIsShare(false) .setIsShare(false)
.setMaxLoginCount(4)
); );
return SaResult.ok("登录成功"); return SaResult.ok("登录成功");
} }