43 lines
868 B
Go
43 lines
868 B
Go
package middleware
|
|
|
|
import (
|
|
"fmt"
|
|
"service/api/base"
|
|
"service/library/logger"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
var (
|
|
DefaultJwtAuthenticator gin.HandlerFunc
|
|
)
|
|
|
|
func InitJwtAuthenticator(verifyFunc func(*gin.Context, string) (int64, error)) {
|
|
DefaultJwtAuthenticator = func(ctx *gin.Context) {
|
|
//1.获取token
|
|
baseRequest, ok := ctx.MustGet("client_req").(base.BaseRequestAccessible)
|
|
if !ok {
|
|
base.CheckBadRequest(ctx, fmt.Errorf("BaseRequest acquire failed"))
|
|
return
|
|
}
|
|
tokenString := baseRequest.GetBaseRequest().Token
|
|
logger.Info("token : %v", tokenString)
|
|
if tokenString == "mastertoken" {
|
|
ctx.Next()
|
|
}
|
|
|
|
//2.校验
|
|
id, err := verifyFunc(ctx, tokenString)
|
|
if !base.CheckBadRequest(ctx, err) {
|
|
return
|
|
}
|
|
|
|
ctx.Set("token_uuid", id)
|
|
ctx.Next()
|
|
}
|
|
}
|
|
|
|
func JwtAuthenticator() gin.HandlerFunc {
|
|
return DefaultJwtAuthenticator
|
|
}
|