update
This commit is contained in:
@ -3,6 +3,7 @@ package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"fashionapi/internal/pkg/jwt"
|
||||
@ -25,21 +26,43 @@ const bearerPrefix = "Bearer "
|
||||
// 令牌签发器由外部注入,避免像原实现那样在每个请求里重新加载一次配置。
|
||||
func Auth(manager *jwt.Manager) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
header := c.GetHeader("Authorization")
|
||||
if !strings.HasPrefix(header, bearerPrefix) {
|
||||
response.AbortError(c, http.StatusUnauthorized, "missing token")
|
||||
return
|
||||
}
|
||||
requireAuth(c, manager)
|
||||
}
|
||||
}
|
||||
|
||||
claims, err := manager.Parse(strings.TrimPrefix(header, bearerPrefix))
|
||||
if err != nil {
|
||||
response.AbortError(c, http.StatusUnauthorized, "invalid token")
|
||||
return
|
||||
}
|
||||
// requireAuth 校验 Bearer token 并把身份信息写入 Context;失败则 401 中断。
|
||||
func requireAuth(c *gin.Context, manager *jwt.Manager) {
|
||||
header := c.GetHeader("Authorization")
|
||||
if !strings.HasPrefix(header, bearerPrefix) {
|
||||
response.AbortError(c, http.StatusUnauthorized, "missing token")
|
||||
return
|
||||
}
|
||||
|
||||
c.Set(ContextUserID, claims.UserID)
|
||||
c.Set(ContextUsername, claims.Username)
|
||||
c.Next()
|
||||
claims, err := manager.Parse(strings.TrimPrefix(header, bearerPrefix))
|
||||
if err != nil {
|
||||
response.AbortError(c, http.StatusUnauthorized, "invalid token")
|
||||
return
|
||||
}
|
||||
|
||||
c.Set(ContextUserID, claims.UserID)
|
||||
c.Set(ContextUsername, claims.Username)
|
||||
c.Next()
|
||||
}
|
||||
|
||||
// PublicFirstPageAuth 列表接口「首页公开、翻页需登录」鉴权中间件。
|
||||
//
|
||||
// 仅当分页参数 page 缺失或 <= 1 时放行(无需 token),其余页要求有效的
|
||||
// Authorization: Bearer <token>,否则返回 401。用于让列表首屏对游客/SEO 可见,
|
||||
// 同时保留翻页 / 批量枚举的登录门槛(防爬虫直接 dump 全量)。
|
||||
func PublicFirstPageAuth(manager *jwt.Manager) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if pageStr := c.Query("page"); pageStr != "" {
|
||||
if n, err := strconv.Atoi(pageStr); err == nil && n > 1 {
|
||||
requireAuth(c, manager) // 非首页:走完整 JWT 校验
|
||||
return
|
||||
}
|
||||
}
|
||||
c.Next() // 首页(page 缺失或 <= 1)放行
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user