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) 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) //2.校验 err := verifyFunc(ctx, tokenString) if !base.CheckBadRequest(ctx, err) { return } ctx.Next() } } func JwtAuthenticator() gin.HandlerFunc { return DefaultJwtAuthenticator }