igolangjwt

package module
v0.0.0-...-92da1fc Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

README

生成密钥

openssl rand -base64 64

生成公钥和私钥

openssl genrsa -out private_key.pem

openssl rsa -in private_key.pem -pubout -out public_key.pem

实力化密钥golangjwt

import (
	"github.com/pkg6/igolangjwt"
)
jwt := igolangjwt.NewMethodHS256("P+lbKgnhxlqbJwU8Xbuvi360buVocyeu/+lNGORh90FyWSjrEAErfLRubK5Oe3rv9rnIm4vQN9HCtuWuvWb2qg==", 7200)

实力化功化公钥和私钥gokangjwt

import (
	"github.com/pkg6/igojwt"
)
jwt := igojwt.NewMethodRS256("private_key.pem", "public_key.pem", 7200)

生成密钥

tokenSrting, err := jwt.CreateToken(golangjwt.NewCustomClaims(1, "zhiqiang"))

解析密钥

CustomClaims,error:= jwt.ParserToken("tokenstring")

刷新密钥

 tokenSrting, err := jwt.UpdateToken("tokenstring")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomClaims

type CustomClaims struct {
	Id   int64  `json:"id"`
	Name string `json:"name"`
	// StandardClaims结构体实现了Claims接口(Valid()函数)
	jwt.RegisteredClaims
}

CustomClaims 定义载荷

func NewCustomClaims

func NewCustomClaims(id int64, name string) *CustomClaims

type GlangJWT

type GlangJWT struct {
	Timestamp  time.Time
	ExpiresIn  int64
	ExpiresAt  time.Time
	PrivateRSA *rsa.PrivateKey
	PublicRSA  *rsa.PublicKey
	// contains filtered or unexported fields
}

func NewMethodHS256

func NewMethodHS256(secret string, expiresIn int64) *GlangJWT

func NewMethodRS256

func NewMethodRS256(privateFileName, publicFileName string, expiresIn int64) *GlangJWT

func (*GlangJWT) CreateToken

func (gj *GlangJWT) CreateToken(claims *CustomClaims) (string, error)

CreateToken 生成token

func (*GlangJWT) ParserToken

func (gj *GlangJWT) ParserToken(tokenString string) (*CustomClaims, error)

ParserToken 解密token

func (*GlangJWT) SetMethod

func (gj *GlangJWT) SetMethod(method jwt.SigningMethod)

SetMethod 设置加密方法

func (*GlangJWT) SetPrivateFileName

func (gj *GlangJWT) SetPrivateFileName(privateFileName string)

SetPrivateFileName 设置公有密钥

func (*GlangJWT) SetPublicFileName

func (gj *GlangJWT) SetPublicFileName(publicFileName string)

SetPublicFileName 设置公有密钥

func (*GlangJWT) SetSecret

func (gj *GlangJWT) SetSecret(secret string)

SetSecret 设置key

func (*GlangJWT) SetTime

func (gj *GlangJWT) SetTime(expiresIn int64)

SetTime 设置过期时间

func (*GlangJWT) UpdateToken

func (gj *GlangJWT) UpdateToken(tokenString string) (string, error)

UpdateToken 更新Token

Jump to

Keyboard shortcuts

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