Using RegisteredClaims to create a token
suggest changeRegisteredClaims represents the claims registered by the JWT specification. It
can be used by itself or embedded in a custom claims type.
mySigningKey := []byte("AllYourBase")
// Create the Claims
claims := &jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Unix(15000, 0)),
Issuer: "test",
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
ss, err := token.SignedString(mySigningKey)
fmt.Printf("%v %v", ss, err)
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDAwLCJpc3MiOiJ0ZXN0In0.QsODzZu3lUZMVdhbO76u3Jv02iYCvEHcYVUI1kOWEU0 <nil>
Real applications should use a strong, randomly generated key and a meaningful expiration time. Never commit a production signing key to source control.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents