修复鉴权:401拦截器加refresh重试 + admin refresh接口 + 路由守卫完善

根因:access_token每2小时过期,前端收到401直接清token跳登录,没有用refresh_token续期

后端修复:
- adminauth模块新增 POST /admin/auth/refresh 接口
- Service 注入 JWTManager,支持 admin refresh token 换新 token pair
- Refresh 方法验证 subjectType=admin + tokenType=refresh

前端修复:
- 401 拦截器核心改造:收到401先调 refresh 接口续期
- 加 isRefreshing 锁 + pendingRequests 队列防止并发刷新
- refresh 用原生 axios.post 避免拦截器递归
- 成功则更新 localStorage + 重试原请求,失败才清 token 跳登录
- 排除 /auth/refresh 自身避免死循环
- 支持 /admin/ 请求独立 token 管理
- auth.ts/adminAuth.ts 新增手动 refreshUserToken/refreshAdminSession
- 路由守卫给所有需登录路由添加 meta.requiresAuth
- 守卫同时支持 PC 端 /login 和移动端 /m/login
This commit is contained in:
yml2213
2026-05-24 07:00:13 +08:00
parent 3631e70321
commit cdee93c7c5
61 changed files with 1735 additions and 722 deletions
+12 -4
View File
@@ -7,10 +7,18 @@ import (
)
type Query struct {
ActorID uint64
Action string
BizType string
Limit int
ActorID uint64
Action string
BizType string
Page int
PageSize int
}
type PaginatedResult struct {
Items interface{} `json:"items"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
}
type LogDTO struct {
+20 -10
View File
@@ -18,17 +18,32 @@ func NewHandler(service *Service) *Handler {
return &Handler{service: service}
}
func parsePagination(c *gin.Context) (int, int) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
if page < 1 {
page = 1
}
if pageSize < 1 {
pageSize = 20
}
if pageSize > 100 {
pageSize = 100
}
return page, pageSize
}
func (h *Handler) List(c *gin.Context) {
query, ok := parseQuery(c)
if !ok {
return
}
items, err := h.service.List(query)
result, err := h.service.List(query)
if err != nil {
writeAuditError(c, err)
return
}
response.OK(c, gin.H{"items": items})
response.OK(c, result)
}
func parseQuery(c *gin.Context) (Query, bool) {
@@ -41,16 +56,11 @@ func parseQuery(c *gin.Context) (Query, bool) {
}
query.ActorID = value
}
if raw := c.Query("limit"); raw != "" {
value, err := strconv.Atoi(raw)
if err != nil || value <= 0 {
response.BadRequest(c, "查询条数不正确")
return query, false
}
query.Limit = value
}
query.Action = c.Query("action")
query.BizType = c.Query("biz_type")
page, pageSize := parsePagination(c)
query.Page = page
query.PageSize = pageSize
return query, true
}
@@ -3,6 +3,8 @@ package adminaudit
import (
"time"
"hfb_sys/backend/internal/model"
"gorm.io/datatypes"
"gorm.io/gorm"
)
@@ -15,37 +17,42 @@ func NewRepository(db *gorm.DB) *Repository {
return &Repository{db: db}
}
func (r *Repository) List(query Query) ([]LogDTO, error) {
limit := query.Limit
if limit <= 0 || limit > 500 {
limit = 200
}
func (r *Repository) List(query Query) (*PaginatedResult, error) {
db := r.db.Table("audit_logs AS al").
Select(`al.id, al.actor_type, al.actor_id, COALESCE(au.username, '') AS actor_username,
COALESCE(au.nickname, '') AS actor_nickname, al.action, al.biz_type, al.biz_id,
al.ip, al.user_agent, al.detail, al.created_at`).
Joins("LEFT JOIN admin_users AS au ON au.id = al.actor_id AND al.actor_type = ?", "admin")
countDB := r.db.Model(&model.AuditLog{})
if query.ActorID > 0 {
db = db.Where("al.actor_id = ?", query.ActorID)
countDB = countDB.Where("actor_id = ?", query.ActorID)
}
if query.Action != "" {
db = db.Where("al.action = ?", query.Action)
countDB = countDB.Where("action = ?", query.Action)
}
if query.BizType != "" {
db = db.Where("al.biz_type = ?", query.BizType)
countDB = countDB.Where("biz_type = ?", query.BizType)
}
var total int64
if err := countDB.Count(&total).Error; err != nil {
return nil, err
}
offset := (query.Page - 1) * query.PageSize
var rows []auditLogRow
if err := db.Order("al.id DESC").Limit(limit).Scan(&rows).Error; err != nil {
if err := db.Order("al.id DESC").Offset(offset).Limit(query.PageSize).Scan(&rows).Error; err != nil {
return nil, err
}
items := make([]LogDTO, 0, len(rows))
for _, row := range rows {
items = append(items, row.toDTO())
}
return items, nil
return &PaginatedResult{Items: items, Total: total, Page: query.Page, PageSize: query.PageSize}, nil
}
type auditLogRow struct {
@@ -12,7 +12,7 @@ func NewService(repo *Repository) *Service {
return &Service{repo: repo}
}
func (s *Service) List(query Query) ([]LogDTO, error) {
func (s *Service) List(query Query) (*PaginatedResult, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}