go get github.com/dgrijalva/jwt-go/v4
github地址:https://github.com/dgrijalva/jwt-go
文档地址:https://pkg.go.dev/github.com/dgrijalva/jwt-go
type myClaims struct { UserNmae string `json:"username"` jwt.StandardClaims } func main() { jwt_key := []byte("yingxiaozhu") // 加密key // 加密一个token claims := myClaims{ UserNmae: "颖小主", StandardClaims: jwt.StandardClaims{ NotBefore: time.Now().Unix() - 60, // 一分钟之前开始生效 ExpiresAt: time.Now().Unix() + 60 * 60 *2, // 两个小时后失效 Issuer: "签发人", // 签发人 }, } token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) token_string, err := token.SignedString(jwt_key) if err != nil { fmt.Println(err.Error()) return } fmt.Println(token_string) // eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IumiluWwj-S4uyIsImV4cCI6MTY0OTk1NDkzOSwiaXNzIjoi562-5Y-R5Lq6IiwibmJmIjoxNjQ5OTQ3Njc5fQ.d8a24gGacP7Af_zy2NdUJvGO1-rHJENZXzV3dA_AESA // 解密token parseToken,err := jwt.ParseWithClaims(token_string, &myClaims{}, func(token *jwt.Token) (interface{}, error) { return jwtToken, nil }) if err != nil { fmt.Println(err.Error()) } fmt.Println(parseToken.Claims.(*myClaims).UserNmae) // 颖小主 }