How to extract the claims from JWT token

后端 未结 1 1769
余生分开走
余生分开走 2021-02-08 05:29

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 : ht

1条回答
  •  梦如初夏
    2021-02-08 06:01

    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
            }
        }
    

    0 讨论(0)
提交回复
热议问题