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

View File

@ -0,0 +1,29 @@
package session
// RedisStore Redis 会话存储(预留实现,切换到 Redis 时启用)
// import "github.com/redis/go-redis/v9"
//
// type RedisStore struct {
// client *redis.Client
// ttl time.Duration
// }
//
// func NewRedisStore(client *redis.Client, ttl time.Duration) *RedisStore {
// return &RedisStore{client: client, ttl: ttl}
// }
//
// func (rs *RedisStore) Get(id string) (*Session, error) { ... }
// func (rs *RedisStore) Set(s *Session) error { ... }
// func (rs *RedisStore) Delete(id string) error { ... }
// func (rs *RedisStore) DeleteByUID(uid uint) error { ... }
// func (rs *RedisStore) Cleanup() int { return 0 }
//
// Redis key pattern:
// session:{sid} → serialized Session (TTL = idle timeout)
// user_sessions:{uid} → Set of sids (for batch deletion)
// 当前使用内存存储Redis 支持预留接口,未来需切换时:
// 1. go get github.com/redis/go-redis/v9
// 2. 取消本文件注释并实现 RedisStore
// 3. 在 deps_core.go 中将 NewMemoryStore → NewRedisStore
// 4. 注入 redis client从 config 读取 Redis 连接信息)