jwt

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2022 License: MIT Imports: 3 Imported by: 1

README

jwt

token生成和验证。


使用示例

	jwt.Init(
		jwt.WithSigningKey("123456"),   // 密钥
		jwt.WithExpire(time.Hour), // 过期时间
		// jwt.WithSigningMethod(jwt.HS512), // 加密方法,默认是HS256,可以设置为HS384、HS512
	)

	uid := "123"
	// 生成token
	token, err := jwt.GenerateToken(uid)

    // 验证token
	v, err := jwt.VerifyToken(token)
	if v.Uid != uid{
	    return
	}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// HS256 Method
	HS256 = jwt.SigningMethodHS256
	// HS384 Method
	HS384 = jwt.SigningMethodHS384
	// HS512 Method
	HS512 = jwt.SigningMethodHS512
)

Functions

func GenerateToken

func GenerateToken(uid string, role ...string) (string, error)

GenerateToken 生成token

func GenerateTokenStandard

func GenerateTokenStandard() (string, error)

GenerateTokenStandard 生成token

func Init

func Init(opts ...Option)

Init 初始化jwt

func VerifyTokenStandard

func VerifyTokenStandard(tokenString string) error

VerifyTokenStandard 验证token

Types

type CustomClaims

type CustomClaims struct {
	UID  string `json:"uid"`
	Role string `json:"role"`
	jwt.StandardClaims
}

CustomClaims 自定义Claims

func VerifyToken

func VerifyToken(tokenString string) (*CustomClaims, error)

VerifyToken 验证token

type Option

type Option func(*options)

Option set the jwt options.

func WithExpire

func WithExpire(d time.Duration) Option

WithExpire set expire value

func WithIssuer

func WithIssuer(issuer string) Option

WithIssuer set issuer value

func WithSigningKey

func WithSigningKey(key string) Option

WithSigningKey set signing key value

func WithSigningMethod

func WithSigningMethod(sm *jwt.SigningMethodHMAC) Option

WithSigningMethod set signing method value

Jump to

Keyboard shortcuts

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