Golang JWT.MAPCLAIMS获取用户ID
设置后,一个简单的关联有许多关联,用户has_many帖子可以使用用户ID创建帖子,这似乎是必要的解析JWT声称以获取用户ID并将其放置在创建后创建中。
因此,如何从JWT声称
我尝试解析令牌,但只出现
map[email:[email protected] exp:1.655701949e+09 username:teste]
tokenString := c.GetHeader("Authorization")
//
claims := jwt.MapClaims{}
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return []byte("supersecretkey"), nil
})
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
fmt.Printf("%v", claims )
} else {
fmt.Println(err)
}
After setup a simple has many association where user has_many posts to create a post with user ID seems that is necessary parse the jwt Claims to get the userID and place it on Post creation.
So, how to get the user ID from jwt Claims
i tried parse the token but just show up
map[email:[email protected] exp:1.655701949e+09 username:teste]
tokenString := c.GetHeader("Authorization")
//
claims := jwt.MapClaims{}
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return []byte("supersecretkey"), nil
})
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
fmt.Printf("%v", claims )
} else {
fmt.Println(err)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从第一个开始告诉你,当你想生成jwt时
喜欢以下内容:
然后当您想解码用户名时,请喜欢以下内容:
I told u from first, when u wanna generate JWT
do like below:
And then when u wanna to decode username do like below: