2025-11-28 23:30:49 +08:00
|
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-12-03 15:27:12 +08:00
|
|
|
|
"carrotskin/internal/model"
|
2025-11-28 23:30:49 +08:00
|
|
|
|
"net/http"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"carrotskin/pkg/auth"
|
2025-12-08 19:12:30 +08:00
|
|
|
|
|
2025-11-28 23:30:49 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
|
// AuthMiddleware JWT认证中间件(注入JWT服务版本)
|
|
|
|
|
|
func AuthMiddleware(jwtService *auth.JWTService) gin.HandlerFunc {
|
2025-11-28 23:30:49 +08:00
|
|
|
|
return gin.HandlerFunc(func(c *gin.Context) {
|
|
|
|
|
|
authHeader := c.GetHeader("Authorization")
|
|
|
|
|
|
if authHeader == "" {
|
2025-12-03 15:27:12 +08:00
|
|
|
|
c.JSON(http.StatusUnauthorized, model.NewErrorResponse(
|
|
|
|
|
|
model.CodeUnauthorized,
|
|
|
|
|
|
"缺少Authorization头",
|
|
|
|
|
|
nil,
|
|
|
|
|
|
))
|
2025-11-28 23:30:49 +08:00
|
|
|
|
c.Abort()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Bearer token格式
|
|
|
|
|
|
tokenParts := strings.SplitN(authHeader, " ", 2)
|
|
|
|
|
|
if len(tokenParts) != 2 || tokenParts[0] != "Bearer" {
|
2025-12-03 15:27:12 +08:00
|
|
|
|
c.JSON(http.StatusUnauthorized, model.NewErrorResponse(
|
|
|
|
|
|
model.CodeUnauthorized,
|
|
|
|
|
|
"无效的Authorization头格式",
|
|
|
|
|
|
nil,
|
|
|
|
|
|
))
|
2025-11-28 23:30:49 +08:00
|
|
|
|
c.Abort()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
token := tokenParts[1]
|
|
|
|
|
|
claims, err := jwtService.ValidateToken(token)
|
|
|
|
|
|
if err != nil {
|
2025-12-03 15:27:12 +08:00
|
|
|
|
c.JSON(http.StatusUnauthorized, model.NewErrorResponse(
|
|
|
|
|
|
model.CodeUnauthorized,
|
|
|
|
|
|
"无效的token",
|
|
|
|
|
|
err,
|
|
|
|
|
|
))
|
2025-11-28 23:30:49 +08:00
|
|
|
|
c.Abort()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 将用户信息存储到上下文中
|
|
|
|
|
|
c.Set("user_id", claims.UserID)
|
|
|
|
|
|
c.Set("username", claims.Username)
|
2025-12-08 19:12:30 +08:00
|
|
|
|
c.Set("user_role", claims.Role)
|
2025-11-28 23:30:49 +08:00
|
|
|
|
|
|
|
|
|
|
c.Next()
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
|
// OptionalAuthMiddleware 可选的JWT认证中间件(注入JWT服务版本)
|
|
|
|
|
|
func OptionalAuthMiddleware(jwtService *auth.JWTService) gin.HandlerFunc {
|
2025-11-28 23:30:49 +08:00
|
|
|
|
return gin.HandlerFunc(func(c *gin.Context) {
|
|
|
|
|
|
authHeader := c.GetHeader("Authorization")
|
|
|
|
|
|
if authHeader != "" {
|
|
|
|
|
|
tokenParts := strings.SplitN(authHeader, " ", 2)
|
|
|
|
|
|
if len(tokenParts) == 2 && tokenParts[0] == "Bearer" {
|
|
|
|
|
|
token := tokenParts[1]
|
|
|
|
|
|
claims, err := jwtService.ValidateToken(token)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
c.Set("user_id", claims.UserID)
|
|
|
|
|
|
c.Set("username", claims.Username)
|
2025-12-08 19:12:30 +08:00
|
|
|
|
c.Set("user_role", claims.Role)
|
2025-11-28 23:30:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
c.Next()
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|