jwt

package
v1.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenToken added in v1.0.1

func GenToken(userId string, username string, expirationTime time.Duration) (string, error)

func GenerateToken added in v1.0.1

func GenerateToken(claims CustomClaims, expirationTime time.Duration) (string, error)

func Init added in v1.0.1

func Init(secretKey string)

Init 使用之前必须先调用Init初始化

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 NewJWT added in v1.0.1

func NewJWT(secretKey string) *JWT

NewJWT 创建一个 JWT 工具实例

func (*JWT) GenerateToken added in v1.0.1

func (j *JWT) GenerateToken(claims CustomClaims, expirationTime time.Duration) (string, error)

GenerateToken 生成一个 token 它接收自定义的声明和过期时间

func (*JWT) ParseToken added in v1.0.1

func (j *JWT) ParseToken(tokenString string) (*CustomClaims, error)

ParseToken 解析和验证一个 token

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL