- 新增 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 到期字段依赖
37 lines
870 B
Go
37 lines
870 B
Go
package middleware
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"metazone.cc/metalab/internal/common"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// AdminAuth 管理后台页面认证:失败时 302 跳首页而非返回 JSON
|
|
func (am *AuthMiddleware) AdminAuth() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
sid, err := c.Cookie(common.SessionCookieName)
|
|
if err != nil || sid == "" {
|
|
common.ClearSessionCookie(c, am.cfg)
|
|
c.Redirect(http.StatusFound, "/")
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
s, err := am.sessionManager.Validate(sid)
|
|
if err != nil || s == nil {
|
|
log.Printf("[AdminAuth] session invalid path=%s → 302", c.Request.URL.Path)
|
|
common.ClearSessionCookie(c, am.cfg)
|
|
c.Redirect(http.StatusFound, "/")
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
log.Printf("[AdminAuth] OK uid=%d role=%s path=%s", s.UserID, s.Role, c.Request.URL.Path)
|
|
injectSessionContext(c, s)
|
|
c.Next()
|
|
}
|
|
}
|