###1. jwt-token的生成和解析
package main
import (
"fmt"
"time"
jwt "github.com/dgrijalva/jwt-go"
)
/**
jwt-token的生成和解析
*/
func main() {
mySigningKey := []byte("hzwy23")
// Create the Claims
claims := &jwt.StandardClaims{
NotBefore: int64(time.Now().Unix() - 1000),
ExpiresAt: int64(time.Now().Unix() + 1000),
Issuer: "test",
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
ss, err := token.SignedString(mySigningKey)
fmt.Println("签名后的token信息:", ss)
t, err := jwt.Parse(ss, func(*jwt.Token) (interface{}, error) {
return mySigningKey, nil
})
if err != nil {
fmt.Println("parase with claims failed.", err)
return
}
fmt.Println("还原后的token信息claims部分:", t.Claims)
}
结果输出:
签名后的token信息: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODU3MTk3MTksImlzcyI6InRlc3QiLCJuYmYiOjE1ODU3MTc3MTl9.q_T9xZW6p2qN1y8ygFroO-OVTZN5rhEZx_iz2D8sjfM
还原后的token信息claims部分: map[exp:1.585719719e+09 iss:test nbf:1.585717719e+09]