XJwt

package
v1.2.8 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2021 License: MIT Imports: 8 Imported by: 0

README

JWT TokenUtils

基于 github.com/dgrijalva/jwt-go 包

Gin Documentation

Documentation https://godoc.org/github.com/dgrijalva/jwt-go

JWT Base Example
tokenString := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJleHAiOjE1MDAwLCJpc3MiOiJ0ZXN0In0.HE7fK0xOQwFEr4WDgRWj4teRPZ6i3GLwD5YCm6Pwu_c"

type MyCustomClaims struct {
    Foo string `json:"foo"`
    jwt.StandardClaims
}

// sample token is expired.  override time so it parses as valid
at(time.Unix(0, 0), func() {
    token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
        return []byte("AllYourBase"), nil
    })

    if claims, ok := token.Claims.(*MyCustomClaims); ok && token.Valid {
        fmt.Printf("%v %v", claims.Foo, claims.StandardClaims.ExpiresAt)
    } else {
        fmt.Println(err)
    }
})
Starter Usage
goinfras.RegisterStarter(XJwt.NewStarter(middlewares...))

Jwt Config Setting
PrivateKey string // jwt编码私钥
ExpSeconds int64  // jwt编码超时时间
UseCache   bool   // 是否打开redis缓存
XJwt Usage
userClaim := UserClaim{Id: "qwertwerhadfsgsadfg", Name: "joker", Avatar: "", Gender: 1}
token, err := XJwt.XTokenUtils().Encode(userClaim)
So(err, ShouldBeNil)
Println("Token String", token)
Println("Token Service Decode And Validate:")
claim, err := XJwt.XTokenUtils().Decode(token)
Println("Validate Error:", err)
So(err, ShouldBeNil)
Println("Token Claim:", claim)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckTku added in v1.1.6

func CheckTku() bool

检查连接池实例

func CreateDefaultTku

func CreateDefaultTku(config *Config)

创建一个默认配置的TokenUtils

func CreateDefaultTkuWithCache added in v1.1.5

func CreateDefaultTkuWithCache(config *Config)

创建一个默认配置的带redis缓存的TokenUtils

func NewStarter

func NewStarter() *starter

func NewTokenUtils

func NewTokenUtils(config *Config) *tokenUtils

func NewTokenUtilsWithCache added in v1.1.5

func NewTokenUtilsWithCache(config *Config) *tokenUtilsCache

func XFTokenUtils

func XFTokenUtils(f func(t ITokenUtils) error) error

资源组件闭包执行

Types

type Config

type Config struct {
	UseCache            bool   // 是否打开redis缓存
	PrivateKey          string // jwt编码私钥
	ExpSeconds          int64  // jwt编码超时时间
	TokenCacheKeyPrefix string // 缓存令牌键名前缀
}

func DefaultConfig

func DefaultConfig() *Config

type CustomerClaim

type CustomerClaim struct {
	UserClaim
	*jwt.StandardClaims
}

聚合jwt内部实现的Claims

type ITokenUtils

type ITokenUtils interface {
	Encode(user UserClaim) (string, error)
	Decode(tokenString string) (*CustomerClaim, error)
	Remove(tokenString string) error
}

func XTokenUtils

func XTokenUtils() ITokenUtils

资源组件实例调用

type UserClaim

type UserClaim struct {
	Id     uint   `json:"id"`
	No     string `json:"no"`
	Name   string `json:"name"`
	Avatar string `json:"avatar"`
}

JWT中携带的用户个人信息

Jump to

Keyboard shortcuts

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