dongque1958 2016-10-04 18:22
浏览 58
已采纳

如何从jwt令牌中提取索赔

I'm using the dgrijalva/jwt-go/ package.

I would like to extract the payload from the token, and I couldn't find a way to do it.


Example (taken from : https://jwt.io/):

for encoded:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ

I would like to extract the payload:

{
  "sub": "1234567890",
  "name": "John Doe",
  "admin": true
}

I will be grateful for an example (using golang).

  • 写回答

1条回答 默认 最新

  • duan0514324 2016-10-04 18:44
    关注

    Sample Code:

        func extractClaims(tokenStr string) (jwt.MapClaims, bool) {
            hmacSecretString := // Value
            hmacSecret := []byte(hmacSecretString)
            token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
                 // check token signing method etc
                 return hmacSecret, nil
            })
    
            if err != nil {
                return nil, false
            }
    
            if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
                return claims, true
            } else {
                log.Printf("Invalid JWT Token")
                return nil, false
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写代码写代码单片机代码写代码写数字按键代码
  • ¥15 django按照距离进行排序
  • ¥15 (标签-微信|关键词-微信公众号)
  • ¥15 matlab中mjs用不了
  • ¥15 Ios抖音直播的时候如何添加自定义图片在直播间!
  • ¥60 riscv-pulpino总线上挂载axi从机
  • ¥15 ssh登录页面的问题
  • ¥50 关于在matlab上对曲柄摇杆机构上一点的运动学仿真
  • ¥15 jetson nano
  • ¥15 :app:debugCompileClasspath'.