123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package utils
- import (
- "fmt"
- "time"
- "github.com/beego/beego/v2/core/logs"
- "github.com/dgrijalva/jwt-go"
- )
- var (
- KEY = []byte("5Mb5Gdmb5x")
- )
- // 发放token
- func GenToken(account string) string {
- token := jwt.New(jwt.SigningMethodHS256)
- token.Claims = &jwt.StandardClaims{
- NotBefore: int64(time.Now().Unix()),
- ExpiresAt: int64(time.Now().Unix() + 90*24*60*60),
- Issuer: "hongze_clpt",
- Subject: account,
- }
- ss, err := token.SignedString(KEY)
- if err != nil {
- logs.Error(err)
- return ""
- }
- return ss
- }
- // 校验token
- func CheckToken(account, token string) bool {
- t, err := jwt.Parse(token, func(*jwt.Token) (interface{}, error) {
- return KEY, nil
- })
- if err != nil {
- fmt.Println(err.Error())
- return false
- }
- if account != t.Claims.(jwt.MapClaims)["sub"] {
- return false
- }
- return t.Valid
- }
|