feat(auth): 实现用户会话数限制功能
Build docker and publish / build (20.15.1) (push) Successful in 7m32s

添加用户会话数限制功能,当超过最大会话数时自动移除最旧的会话
- 在config中添加UserSessionsKeyPrefix常量
- 在JwtAuth配置中新增MaxSessionsPerUser字段
- 在ServiceContext中实现EnforceUserSessionLimit方法
- 在所有登录逻辑中调用会话限制检查
This commit is contained in:
2025-11-26 17:52:12 -08:00
parent 4ad384b01a
commit 1d5d361ae8
8 changed files with 167 additions and 29 deletions
+3
View File
@@ -111,6 +111,9 @@ func (l *UserLoginLogic) UserLogin(req *types.UserLoginRequest) (resp *types.Log
l.Logger.Error("[UserLogin] token generate error", logger.Field("error", err.Error()))
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "token generate error: %v", err.Error())
}
if err = l.svcCtx.EnforceUserSessionLimit(l.ctx, userInfo.Id, sessionId, l.svcCtx.Config.JwtAuth.MaxSessionsPerUser); err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "enforce session limit error: %v", err.Error())
}
sessionIdCacheKey := fmt.Sprintf("%v:%v", config.SessionIdKey, sessionId)
if err = l.svcCtx.Redis.Set(l.ctx, sessionIdCacheKey, userInfo.Id, time.Duration(l.svcCtx.Config.JwtAuth.AccessExpire)*time.Second).Err(); err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "set session id error: %v", err.Error())