golangjwt

package module
v0.0.0-...-8c2ac6e Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2023 License: Apache-2.0 Imports: 6 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

实力化密钥jwt

import (
    "github.com/pkg6/golang-jwt"
)
jwt := golangjwt.NewExpiresMethodHS256("P+lbKgnhxlqbJwU8Xbuvi360buVocyeu/+lNGORh90FyWSjrEAErfLRubK5Oe3rv9rnIm4vQN9HCtuWuvWb2qg==", 7200)

实力化公钥和私钥jwt

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

生成密钥

token, err := jwt.CreateToken(jwt4.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 Jwts

type Jwts struct {
	Method     jwt.SigningMethod
	Secret     []byte
	Timestamp  time.Time
	ExpiresIn  int64
	ExpiresAt  time.Time
	PrivateRSA *rsa.PrivateKey
	PublicRSA  *rsa.PublicKey
}

func NewExpiresMethodHS256

func NewExpiresMethodHS256(secret string, expiresIn int64) *Jwts

func NewExpiresMethodRS256

func NewExpiresMethodRS256(privateFileName, publicFileName string, expiresIn int64) *Jwts

func NewMethodHS256

func NewMethodHS256(secret string) *Jwts

func NewMethodRS256

func NewMethodRS256(privateFileName, publicFileName string) *Jwts

func (*Jwts) CreateToken

func (j *Jwts) CreateToken(claims *CustomClaims) (string, error)

CreateToken 生成token

func (*Jwts) ParserToken

func (j *Jwts) ParserToken(tokenString string) (*CustomClaims, error)

ParserToken 解密token

func (*Jwts) SetMethod

func (j *Jwts) SetMethod(method jwt.SigningMethod)

SetMethod 设置加密方法

func (*Jwts) SetPrivateFileName

func (j *Jwts) SetPrivateFileName(privateFileName string)

SetPrivateFileName 设置公有密钥

func (*Jwts) SetPublicFileName

func (j *Jwts) SetPublicFileName(publicFileName string)

SetPublicFileName 设置公有密钥

func (*Jwts) SetSecret

func (j *Jwts) SetSecret(secret string)

SetSecret 设置key

func (*Jwts) SetTime

func (j *Jwts) SetTime(expiresIn int64)

SetTime 设置过期时间

func (*Jwts) UpdateToken

func (j *Jwts) 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