jwt

package
v0.0.0-...-90bc274 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TokenExpireDuration        = time.Hour * 24 * 2
	RefreshTokenExpireDuration = time.Hour * 24 * 7
)

Variables

View Source
var (
	ErrTokenExpired = errors.New("token过期")
)

Functions

func GenToken

func GenToken(userID uint64, userName string, isAdmin bool) (aToken, rToken string, err error)

GenToken 生成access token 和 refresh token

func GenTokenWithExpire

func GenTokenWithExpire(userID uint64, userName string, isAdmin bool,
	tokenExpire time.Duration, rfTokenExpire time.Duration) (aToken, rToken string, err error)

func RefreshToken

func RefreshToken(aToken, rToken string) (newAToken, newRToken string, err error)

RefreshToken 刷新AccessToken

func TokenExpire

func TokenExpire() int64

Types

type MyClaims

type MyClaims struct {
	UserID   uint64 `json:"userID"`
	UserName string `json:"userName"`
	IsAdmin  bool   `json:"isAdmin"`
	jwt.StandardClaims
}

MyClaims 自定义声明结构体并内嵌jwt.StandardClaims jwt包自带的jwt.StandardClaims只包含了官方字段 我们这里需要额外记录一个UserID字段,所以要自定义结构体 如果想要保存更多信息,都可以添加到这个结构体中

func ParseToken

func ParseToken(tokenString string) (claims *MyClaims, err error)

ParseToken 解析JWT

Jump to

Keyboard shortcuts

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