package common import ( "github.com/dgrijalva/jwt-go" ) // 载荷,增加用户别名 type Claims struct { UserId uint64 jwt.StandardClaims } func SetJWTSecret(secret string) { jwtSecret = []byte(secret) } var jwtSecret []byte //解析token func ParseToken(token string) (*Claims, error) { tokenClaims, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (interface{}, error) { return jwtSecret, nil }) if err != nil { return nil, err } if tokenClaims != nil { claims, ok := tokenClaims.Claims.(*Claims) if ok && tokenClaims.Valid { return claims, nil } } return nil, err }