为什么我的 Echo JWT 自定义代码不起作用?

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

Why is my echo jwt custom code not working?

问题

我正在使用echo jwt验证用户,我正在根据手册进行自定义,但没有应用。我有两个要求。

  1. 我想在没有"bearer"关键字的情况下进行令牌检查。

  2. 当找不到令牌或令牌无效时,我想返回适当的错误消息。

如何修改上述代码以实现我的要求?

我刚开始学习golang,任何建议都将不胜感激。

  1. output := echojwt.JWT(&echojwt.Config{
  2. SigningKey: []byte(key.EnvSecretKey),
  3. TokenLookup: "header:Authorization",
  4. ErrorHandler: func(c echo.Context, err error) error {
  5. if err != nil {
  6. return c.JSON(400, "custom error")
  7. }
  8. return nil
  9. },
  10. })

以下是修改后的代码:

  1. output := echojwt.JWT(&echojwt.Config{
  2. SigningKey: []byte(key.EnvSecretKey),
  3. TokenLookup: "header:Authorization",
  4. ErrorHandler: func(c echo.Context, err error) error {
  5. if err != nil {
  6. return c.JSON(400, map[string]string{"error": "自定义错误消息"})
  7. }
  8. return nil
  9. },
  10. AuthScheme: "",
  11. })

通过将AuthScheme设置为空字符串,你可以在没有"bearer"关键字的情况下进行令牌检查。另外,你可以返回一个包含自定义错误消息的JSON响应。

英文:

I am verifying the user using echo jwt, I am customizing while looking at the manual, but it is not applied. There are two things I want.

  1. I want to proceed token check without bearer keyword

  2. I want to return an error message appropriate to the situation when the token is not found or the token is invalid.

How can I modify the above code to achieve what I want?

I'm just starting to learn golang, any advice would be appreciated

  1. output := echojwt.JWT(&echojwt.Config{
  2. SigningKey: []byte(key.EnvSecretKey),
  3. TokenLookup: "header:Authorization",
  4. ErrorHandler: func(c echo.Context, err error) error {
  5. if err != nil {
  6. return c.JSON(400, "custom error")
  7. }
  8. return nil
  9. },
  10. })

答案1

得分: 2

以下是你要翻译的内容:

这里是你可以按照的步骤:

  1. 在没有 "bearer" 关键字的情况下进行令牌检查:为了做到这一点,你可以将 TokenLookup 的值从 "header:Authorization" 改为 "query:token"。这样可以将令牌作为查询参数传递,而不是在头部中传递。

  2. 当找不到令牌或令牌无效时返回适当的自定义错误消息:为了做到这一点,你可以修改错误处理函数。

以下是一个示例:

output := echojwt.JWT(&echojwt.Config{
SigningKey: []byte(key.EnvSecretKey),
TokenLookup: "query:token",
ErrorHandler: func(c echo.Context, err error) error {
if err == jwt.ErrTokenNotFound {
return c.JSON(http.StatusBadRequest, "找不到令牌")
}
if ve, ok := err.(*jwt.ValidationError); ok {
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
return c.JSON(http.StatusBadRequest, "令牌格式错误")
} else if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 {
return c.JSON(http.StatusUnauthorized, "令牌已过期或尚未生效")
} else {
return c.JSON(http.StatusBadRequest, "令牌无效")
}
}
return nil
},
})

英文:

Here is the step that you can follows:

  1. Proceed token check without the "bearer" keyword: To do this, you
    can change the TokenLookup value from "header:Authorization" to
    "query:token". This will allow you to pass the token as a query
    parameter instead of in the header.

  2. Return a custom error message appropriate to the situation when the
    token is not found or the token is invalid: To do this, you can
    modify the error handler function

Here's an example:

  1. output := echojwt.JWT(&echojwt.Config{
  2. SigningKey: []byte(key.EnvSecretKey),
  3. TokenLookup: "query:token",
  4. ErrorHandler: func(c echo.Context, err error) error {
  5. if err == jwt.ErrTokenNotFound {
  6. return c.JSON(http.StatusBadRequest, "token not found")
  7. }
  8. if ve, ok := err.(*jwt.ValidationError); ok {
  9. if ve.Errors&jwt.ValidationErrorMalformed != 0 {
  10. return c.JSON(http.StatusBadRequest, "token is malformed")
  11. } else if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 {
  12. return c.JSON(http.StatusUnauthorized, "token is expired or not valid yet")
  13. } else {
  14. return c.JSON(http.StatusBadRequest, "token is invalid")
  15. }
  16. }
  17. return nil
  18. },
  19. })

答案2

得分: 0

总结一下,我使用了错误的方法,

  1. echojwt.JWT(&echojwt.Config{

而应该使用

  1. echojwt.WithConfig(echojwt.Config{

使用正确的方法后,它正常工作。

至于错误处理,kha的回答很有帮助。

英文:

In conclusion, I used the wrong method,

  1. echojwt.JWT(&echojwt.Config{

instead

  1. echojwt.WithConfig(echojwt.Config{

When using , it works normally.

As for the error handle, kha's answer was helpful.

huangapple
  • 本文由 发表于 2023年2月10日 00:41:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75401757.html
匿名

发表评论

匿名网友

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

确定