echo 跳过 JWT 中间件

Echo skip JWT middleware

我用的是golang jwt中间件

e.Use(middleware.JWTWithConfig(middleware.JWTConfig{
  SigningKey:  []byte(os.Getenv("Signing_Key")),
  TokenLookup: "header:x-auth-token",       
}))

它等待所有功能的令牌,但我不想将此中间件用于登录功能。如何预防?

有一个skipper function。您可以使用它来检查要跳过的路线。

JWTConfig struct {
  // Skipper defines a function to skip middleware.
  Skipper Skipper
  ... 
}

检查一个例子:

e.Use(middleware.JWTWithConfig(middleware.JWTConfig{
    SigningKey:  []byte(os.Getenv("Signing_Key")),
    TokenLookup: "header:x-auth-token",
    Skipper: func(c echo.Context) bool {
       // Skip middleware if path is equal 'login'
       if c.Request().URL.Path == "/login" {
         return true
       }
       return false
    },
}))