feat 网关鉴权
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
package http_gateway
|
||||
|
||||
import (
|
||||
"common/net/http/http_resp"
|
||||
"common/utils"
|
||||
"fmt"
|
||||
"gateway/config"
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -35,3 +40,28 @@ func ginLogger(logger *zap.SugaredLogger) gin.HandlerFunc {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func authJwt() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
if authHeader == "" {
|
||||
http_resp.AbortUnauthorized(c)
|
||||
return
|
||||
}
|
||||
|
||||
parts := strings.Split(authHeader, " ")
|
||||
if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" {
|
||||
http_resp.AbortUnauthorized(c)
|
||||
return
|
||||
}
|
||||
|
||||
claims, err := utils.ParseToken(parts[1], config.Get().Auth.Secret)
|
||||
if err != nil {
|
||||
http_resp.AbortUnauthorized(c)
|
||||
return
|
||||
}
|
||||
|
||||
c.Request.Header.Set("X-Usn", strconv.Itoa(int(claims.USN)))
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user