token

package
v0.0.0-...-2324c52 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MulanPSL-2.0 Imports: 1 Imported by: 0

README

token

用于生成用户在前端页面中的Token字段。

接口:

  • GenerateToken:生成一个新的Token信息。
  • ParseToken:解析已有的Token信息。
Token

例如声明一个jwt.Claims接口的实现:

type CustomClaims struct {
	jwt.StandardClaims
	UserId     uint64 `json:"uid"` // 员工ID
	Env        string `json:"ev"`  // 环境信息
	ExpireTime int64  `json:"et"`  // 有效截止时间的时间戳
}

调用生成Token函数:

claims := CustomClaims{UserId: 1, Env: "web", ExpireTime: time.Now().Unix()}
token, err := GenerateToken[*CustomClaims](&claims, "1234567890")

调用解析Token函数:

var gotClaims CustomClaims
err = ParseToken[*CustomClaims](token, "1234567890", &gotClaims)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateToken

func GenerateToken[T jwt.Claims](claim T, secretKey string) (tokenString string, err error)

生成Token

func ParseToken

func ParseToken[T jwt.Claims](tokenString string, secretKey string, targetClaims T) (err error)

解析Token

Types

This section is empty.

Jump to

Keyboard shortcuts

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