英文:
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"
}
}))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论