jwt

package
v0.0.0-...-17112b8 Latest Latest
Warning

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

Go to latest
Published: May 6, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var JWT = func(ctx *context.Context) {
	var code int
	var data interface{}
	code = e.SUCCESS
	Authorization := ctx.Input.Header("Authorization")
	fmt.Println(Authorization)
	token := strings.Split(Authorization, " ")
	fmt.Println(token)
	if Authorization == "" {
		code = e.NO_AUTH
		fmt.Println(Authorization)
	} else {
		fmt.Println("token", token[1])
		_, err := ParseToken(token[1])
		if err != nil {
			switch err.(*jwt.ValidationError).Errors {
			case jwt.ValidationErrorExpired:
				code = e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT
			default:
				code = e.ERROR_AUTH_CHECK_TOKEN_FAIL
			}
		}
	}
	if code != e.SUCCESS {

		ResponseWithJson(ctx.ResponseWriter, http.StatusBadRequest,
			Response{Code: code, Msg: e.GetMsg(code), Data: data})
		return
	}

}

验证token拦截器

View Source
var JwtSecret = []byte("jcops")

Functions

func GenerateToken

func GenerateToken(username, password string) (string, error)

生成Jwt Token

func GetIdFromClaims

func GetIdFromClaims(key string, claims jwt.Claims) string

func ResponseWithJson

func ResponseWithJson(w http.ResponseWriter, code int, payload interface{})

Types

type Claims

type Claims struct {
	Username string `json:"username"`
	Password string `json:"password"`
	jwt.StandardClaims
}

func ParseToken

func ParseToken(token string) (*Claims, error)

验证JWT Token

type Response

type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

Jump to

Keyboard shortcuts

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