You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1.5 KiB
75 lines
1.5 KiB
package utils
|
|
|
|
import (
|
|
"appNewPlatform/appConstant"
|
|
"appNewPlatform/generalmethod"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt"
|
|
)
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 2024-10-03 13:36:08
|
|
@ 功能: 获取Token
|
|
@ 参数
|
|
|
|
#
|
|
|
|
@ 返回值
|
|
|
|
#
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
func ReleaseToken(userKey string) (token string, err error) {
|
|
expirationTime := time.Now().Add(7 * 24 * time.Hour) //过期时间
|
|
claims := &Claims{
|
|
UserId: userKey,
|
|
UUID: strconv.FormatInt(generalmethod.GetUUid(2), 10),
|
|
StandardClaims: jwt.StandardClaims{
|
|
ExpiresAt: expirationTime.Unix(),
|
|
IssuedAt: time.Now().Unix(), //token发放的时间
|
|
Issuer: "admin", //发放人
|
|
Subject: "AppJwtToken", //主题
|
|
},
|
|
}
|
|
// fmt.Printf("tokenJwt:%v--->%v\n", jwt.SigningMethodHS256, err)
|
|
tokenJwt := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
// fmt.Printf("tokenJwt:%v--->%v\n", tokenJwt, err)
|
|
token, err = tokenJwt.SignedString([]byte(appConstant.Constant_Config.Appsetup.AppKey))
|
|
// fmt.Printf("tokenJwt:%v--->%v\n", appConstant.Constant_Config.Appsetup.AppKey, err)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return
|
|
}
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 2024-10-03 14:41:32
|
|
@ 功能: 解析Jwt信息
|
|
@ 参数
|
|
|
|
#tokenString token字符串
|
|
|
|
@ 返回值
|
|
|
|
#
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
func ParseToken(tokenString string) (*jwt.Token, *Claims, error) {
|
|
claims := &Claims{}
|
|
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
|
|
return []byte(appConstant.Constant_Config.Appsetup.AppKey), nil
|
|
})
|
|
return token, claims, err
|
|
}
|
|
|