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:
@ -7,19 +7,21 @@ import (
|
||||
"metazone.cc/metalab/internal/common"
|
||||
"metazone.cc/metalab/internal/config"
|
||||
"metazone.cc/metalab/internal/model"
|
||||
"metazone.cc/metalab/internal/session"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// MaintenanceMiddleware 维护模式中间件:维护期间仅站长(Owner)可访问
|
||||
type MaintenanceMiddleware struct {
|
||||
cfg *config.Config
|
||||
siteSettings *config.SiteSettings
|
||||
cfg *config.Config
|
||||
siteSettings *config.SiteSettings
|
||||
sessionManager *session.Manager
|
||||
}
|
||||
|
||||
// NewMaintenanceMiddleware 构造函数
|
||||
func NewMaintenanceMiddleware(cfg *config.Config, siteSettings *config.SiteSettings) *MaintenanceMiddleware {
|
||||
return &MaintenanceMiddleware{cfg: cfg, siteSettings: siteSettings}
|
||||
func NewMaintenanceMiddleware(cfg *config.Config, siteSettings *config.SiteSettings, sm *session.Manager) *MaintenanceMiddleware {
|
||||
return &MaintenanceMiddleware{cfg: cfg, siteSettings: siteSettings, sessionManager: sm}
|
||||
}
|
||||
|
||||
// Handler 返回 Gin 中间件处理函数
|
||||
@ -39,51 +41,43 @@ func (mm *MaintenanceMiddleware) Handler() gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
// 尝试读取 token 并解析角色
|
||||
tokenStr, err := c.Cookie(common.CookieName)
|
||||
if err != nil {
|
||||
// 未登录 → 重定向到登录页(API 返回 403)
|
||||
// 读取 session cookie 并验证
|
||||
sid, err := c.Cookie(common.SessionCookieName)
|
||||
if err != nil || sid == "" {
|
||||
blockAccess(c, path)
|
||||
return
|
||||
}
|
||||
|
||||
claims, err := parseToken(tokenStr, mm.cfg.JWT.Secret)
|
||||
if err != nil {
|
||||
s, err := mm.sessionManager.Validate(sid)
|
||||
if err != nil || s == nil {
|
||||
blockAccess(c, path)
|
||||
return
|
||||
}
|
||||
|
||||
role, _ := claims["role"].(string)
|
||||
if !model.HasMinRole(role, model.RoleOwner) {
|
||||
if !model.HasMinRole(s.Role, model.RoleOwner) {
|
||||
blockAccess(c, path)
|
||||
return
|
||||
}
|
||||
|
||||
// 注入用户信息到 context(后续 auth 中间件可能还需要)
|
||||
injectUserContext(c, claims)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// isMaintenanceWhitelist 判断路径是否在维护白名单中
|
||||
func isMaintenanceWhitelist(path string) bool {
|
||||
// 登录页面和认证 API
|
||||
if path == "/auth/login" || strings.HasPrefix(path, "/api/auth/") {
|
||||
return true
|
||||
}
|
||||
// 静态资源
|
||||
if strings.HasPrefix(path, "/static/") || strings.HasPrefix(path, "/shared/") {
|
||||
return true
|
||||
}
|
||||
// favicon
|
||||
if path == "/favicon.ico" || path == "/favicon.svg" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// blockAccess 根据请求类型阻止访问(页面重定向,API 返回 403 JSON)
|
||||
// blockAccess 根据请求类型阻止访问
|
||||
func blockAccess(c *gin.Context, path string) {
|
||||
if strings.HasPrefix(path, "/api/") || strings.HasPrefix(path, "/admin/api/") {
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
|
||||
|
||||
Reference in New Issue
Block a user