Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateToken ¶ added in v1.0.1
func GenerateToken(claims CustomClaims, expirationTime time.Duration) (string, error)
Types ¶
type CustomClaims ¶ added in v1.0.1
type CustomClaims struct {
UserId string `json:"userId"`
Username string `json:"username"`
jwt.RegisteredClaims
}
CustomClaims 自定义声明类型,内嵌 jwt.RegisteredClaims RegisteredClaims 包含官方定义的标准字段:(iss, sub, aud, exp, nbf, iat, jti) 我们这里增加了自定义字段 UserID 和 Username
func ParseToken ¶ added in v1.0.1
func ParseToken(tokenString string) (*CustomClaims, error)
type JWT ¶ added in v1.0.1
type JWT struct {
// SecretKey 用于签名的密钥,应该是保密的
SecretKey []byte
}
JWT 是我们的 JWT 工具结构体
func (*JWT) GenerateToken ¶ added in v1.0.1
GenerateToken 生成一个 token 它接收自定义的声明和过期时间
func (*JWT) ParseToken ¶ added in v1.0.1
func (j *JWT) ParseToken(tokenString string) (*CustomClaims, error)
ParseToken 解析和验证一个 token
Click to show internal directories.
Click to hide internal directories.