代码拉取完成,页面将自动刷新
package tokenutil
import (
"gitee.com/zhongguo168a/gocodes/datax/mapx"
"github.com/dgrijalva/jwt-go"
"github.com/pkg/errors"
"time"
)
var (
ErrParseFail = errors.New("token parse fail")
ErrIllegal = errors.New("token invalid")
ErrExpired = errors.New("token expired")
ErrUseNotmatch = errors.New("token use notmatch")
)
// CreateLater 令牌-创建
func CreateToken(info *TokenInfo, hmacSampleSecret string) (t string, err error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"uid": info.Uid,
"role": info.Role,
"time": int(time.Now().Unix()),
})
t, err = token.SignedString([]byte(hmacSampleSecret))
if err != nil {
err = errors.Wrap(err, "signed string")
return
}
return
}
func ParseToken(tok, secret string) (info *TokenInfo, err error) {
t, err := jwt.Parse(tok, func(*jwt.Token) (interface{}, error) {
return []byte(secret), nil
})
if err != nil {
return nil, ErrParseFail
}
info = &TokenInfo{}
info.JwtToken = t
var tokenMap = t.Claims.(jwt.MapClaims)
info.Uid = mapx.String(tokenMap, "uid")
info.Role = mapx.String(tokenMap, "role")
info.Time = int32(mapx.Int(tokenMap, "time"))
return
}
// TokenInfo 令牌信息
type TokenInfo struct {
// 用户id
Uid string
// 权限
Role string
//
Time int32
//
JwtToken *jwt.Token
}
func (info *TokenInfo) GetLogObj() (r map[string]interface{}) {
r = map[string]interface{}{}
if info == nil {
return
}
r["uid"] = info.Uid
r["role"] = info.Role
r["time"] = info.Time
return
}
// Valid 验证token
func (info *TokenInfo) Valid(expiredTime int) (err error) {
if info.JwtToken.Valid == false {
err = ErrIllegal
return
}
if time.Now().Unix() > time.Unix(int64(info.Time), 0).Add(time.Duration(int(time.Second)*expiredTime)).Unix() {
err = ErrExpired
return
}
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。