refactor: JWT 无状态认证替换为服务端 Session,修复记住我掉线问题

- 新增 internal/session 包:Session 结构体、Store 接口、MemoryStore(内存+后台清理)、RedisStore 预留
- Cookie 从 3 个精简为 1 个 mlb_sid(HttpOnly),移除 JWT access/refresh cookie
- 会话过期采用滑动窗口:每次请求自动续期,记住我 30 天无操作过期
- AuthMiddleware/AuthAdmin/Maintenance 中间件改用 SessionManager.Validate()
- AuthService Login/Register/ConfirmRestore 去除 token 生成,返回 *model.User
- Controller 层在登录/注册后调用 SessionManager.Create 创建会话
- AdminService UpdateUserStatus/ResetUserToken 同步销毁 session 实现即时退登
- 改密/注销时通过 DestroyByUID 删除所有 session(替换 TokenVersion 检查)
- config.yaml 新增 session 配置段(idle_timeout/remember_timeout/cleanup_interval)
- TokenService/auth_parser/auth_token 标记废弃,移除 JWT 到期字段依赖
This commit is contained in:
2026-05-30 23:36:52 +08:00
parent 53f84bfcc4
commit daf87f895b
28 changed files with 617 additions and 482 deletions

19
internal/session/store.go Normal file
View File

@ -0,0 +1,19 @@
package session
// Store 会话存储接口(内存实现 / Redis 实现均实现此接口)
type Store interface {
// Get 按会话 ID 获取会话,不存在或已过期返回 nil
Get(id string) (*Session, error)
// Set 写入/更新会话
Set(s *Session) error
// Delete 删除单个会话
Delete(id string) error
// DeleteByUID 删除某用户的所有会话(改密/注销/强制下线时调用)
DeleteByUID(uid uint) error
// Cleanup 清理过期会话,返回清理数量(由后台 goroutine 定期调用)
Cleanup() int
}