1 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊
通過(guò)檢查您的代碼,我猜您正在使用這個(gè)golang-jwt包。
調(diào)用后,jwt.ParseWithClaims您應(yīng)該能夠訪問(wèn)您對(duì)代碼中jwt.MapClaims命名的已創(chuàng)建對(duì)象的聲明。claims
沒(méi)有 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 關(guān)注
- 138 瀏覽
添加回答
舉報(bào)