2026-02-09 14:33:41 +08:00
|
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"net/http"
|
2026-02-10 14:15:32 +08:00
|
|
|
|
|
|
|
|
|
|
"soul-api/internal/auth"
|
|
|
|
|
|
"soul-api/internal/config"
|
2026-02-09 14:33:41 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-02-10 15:03:31 +08:00
|
|
|
|
// AdminAuth 管理端鉴权:校验 JWT(Authorization: Bearer 或 Cookie admin_session),未登录返回 401
|
2026-02-09 14:33:41 +08:00
|
|
|
|
func AdminAuth() gin.HandlerFunc {
|
|
|
|
|
|
return func(c *gin.Context) {
|
2026-02-10 14:15:32 +08:00
|
|
|
|
cfg := config.Get()
|
|
|
|
|
|
if cfg == nil {
|
2026-02-09 14:33:41 +08:00
|
|
|
|
c.Next()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-02-10 15:03:31 +08:00
|
|
|
|
token := auth.GetAdminJWTFromRequest(c.Request)
|
|
|
|
|
|
if _, ok := auth.ParseAdminJWT(token, cfg.AdminSessionSecret); !ok {
|
2026-02-10 14:15:32 +08:00
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"success": false, "error": "未授权访问,请先登录"})
|
2026-02-09 14:33:41 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
c.Next()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|