jwt

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

README

jwt

定义一个接口体,继承StandardClaims,并且实现Claims接口, 快速生成非对称加密串

type MyStruct struct {
	Name string `json:"name"`
	StandardClaims
}

func (*MyStruct) ExpiresAt() time.Duration {
	return time.Second
}

func (*MyStruct) Secret() string {
	return "xxxxx"
}
func TestEncryption(t *testing.T) {
	myStruct := MyStruct{Name: "zhangguilin"}
	encryption, err := Encryption(&myStruct)
	if err != nil {
		t.Error(err)
		return
	}
	fmt.Println(encryption)
	for true {
		myStruct.Name = ""
		err = Decryption(&myStruct, encryption)
		fmt.Println(err,myStruct)
		time.Sleep(time.Millisecond * 200)
	}
}

OutPut:

<nil> {go landscape { 1616899118  0  0 }}
<nil> {go landscape { 1616899118  0  0 }}
<nil> {go landscape { 1616899118  0  0 }}
<nil> {go landscape { 1616899118  0  0 }}
<nil> {go landscape { 1616899118  0  0 }}
token is expired by 1s {go landscape { 1616899118  0  0 }}
token is expired by 1s {go landscape { 1616899118  0  0 }}
token is expired by 1s {go landscape { 1616899118  0  0 }}
token is expired by 1s {go landscape { 1616899118  0  0 }}
token is expired by 1s {go landscape { 1616899118  0  0 }}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decryption

func Decryption(claims Claims, token string) error

Decryption 解密该jwt token,并且映射到实现了 Claims 接口的实例

func Encryption

func Encryption(claims Claims) (string, error)

Encryption 将实现了 Claims 的结构体生成对应的 jwt token

Types

type Claims

type Claims interface {
	//ExpiresAt 经过多长时间过期
	ExpiresAt() time.Duration
	//Secret 秘钥
	Secret() string
	//继承 jwt.Claims 接口, 该接口在继承 StandardClaims 时自动实现
	jwt.Claims
}

Claims 定义生成jwt token的方法

type StandardClaims

type StandardClaims = jwt.StandardClaims

Jump to

Keyboard shortcuts

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