回声跳过JWT中间件

huangapple go评论75阅读模式
英文:

Echo skip JWT middleware

问题

我使用了Golang的JWT中间件。

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

它会在所有函数中等待令牌,但我不想在登录函数中使用这个中间件。如何防止这种情况发生?

英文:

I used golang jwt middleware.

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

It waits token for all functions but I don't want to use this middleware for login function. How to prevent this?

答案1

得分: 10

有一个skipper函数。您可以使用它来检查要跳过的路由。

JWTConfig结构体 {
  // Skipper定义了一个跳过中间件的函数。
  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 {
       // 如果路径等于'login',则跳过中间件
       if c.Request().URL.Path == "/login" {
	     return true
       }
       return false
    },
}))
英文:

There is a skipper function. You can use it to check which route to skip.

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

Check an example:

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
    },
}))

答案2

得分: 1

可以像这样:

e.Use(middleware.JWTWithConfig(middleware.JWTConfig{
    SigningKey:  []byte(os.Getenv("Signing_Key")),
    TokenLookup: "header:x-auth-token",
    Skipper: func(c echo.Context) bool {
       return c.Request().URL.Path == "/login"
    }
}))
英文:

It could be something like this

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

huangapple
  • 本文由 发表于 2022年1月6日 00:31:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/70596344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定