1 回答
TA贡献1827条经验 获得超4个赞
通过检查您的代码,我猜您正在使用这个golang-jwt包。
调用后,jwt.ParseWithClaims您应该能够访问您对代码中jwt.MapClaims命名的已创建对象的声明。claims
没有 Fiber 的例子,只有 golang-jwt,但逻辑保持不变:
package main
import (
"fmt"
"github.com/golang-jwt/jwt/v4"
)
// Created on https://jwt.io/
const tokenString = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InRlc3RAbWFpbC5jaCJ9.Iv6oZWIX7Rrtag4d6h3-eJ3xdXLwoZ9PbotcvbjOvhI"
// Just as an example, make sure to pick a stronger key
const key = "1234567890"
func main() {
claims := jwt.MapClaims{}
token, err := jwt.ParseWithClaims(tokenString, claims, keyFunc)
if err != nil {
panic(err)
}
fmt.Println(token.Valid)
email, ok := claims["email"].(string)
if !ok {
panic("Couldn't parse email as string")
}
fmt.Println(email)
}
func keyFunc(*jwt.Token) (interface{}, error) {
return []byte(key), nil
}
- 1 回答
- 0 关注
- 110 浏览
添加回答
举报