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.
155 lines
2.9 KiB
155 lines
2.9 KiB
package utils
|
|
|
|
import (
|
|
"appNewPlatform/appConstant"
|
|
"appNewPlatform/generalmethod"
|
|
"appNewPlatform/utils/formatoutput"
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"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字符串
|
|
|
|
@ 返回值
|
|
|
|
#jwt.Token
|
|
#jwt.Token
|
|
#error
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
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
|
|
}
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 2025-02-13 10:44:02
|
|
@ 功能: 验证jwt令牌
|
|
@ 参数
|
|
|
|
#tokenString 令牌
|
|
|
|
@ 返回值
|
|
|
|
#Claims 解析后数据
|
|
#error 信息
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
func ValidateToken(tokenString string) (*Claims, error) {
|
|
token, claims, err := ParseToken(tokenString)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !token.Valid {
|
|
return nil, fmt.Errorf("无效的令牌")
|
|
}
|
|
if time.Now().Unix() > claims.ExpiresAt {
|
|
return nil, fmt.Errorf("令牌已过期")
|
|
}
|
|
return claims, nil
|
|
}
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 2025-02-13 11:36:59
|
|
@ 功能:
|
|
@ 参数
|
|
|
|
#
|
|
|
|
@ 返回值
|
|
|
|
#
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
func AuthMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
tokenString := c.GetHeader("Authorization")
|
|
if tokenString == "" {
|
|
formatoutput.Result(300, tokenString, c, "未获得授权!")
|
|
c.Abort()
|
|
return
|
|
}
|
|
if len(tokenString) > 7 && tokenString[:7] == "Bearer " {
|
|
tokenString = tokenString[7:]
|
|
} else {
|
|
formatoutput.Result(300, tokenString, c, "授权标头无效!")
|
|
c.Abort()
|
|
return
|
|
}
|
|
_, claims, err := ParseToken(tokenString)
|
|
if err != nil {
|
|
formatoutput.Result(300, tokenString, c, "无效的令牌!")
|
|
c.Abort()
|
|
return
|
|
}
|
|
c.Set("username", claims.UserId)
|
|
c.Next()
|
|
}
|
|
}
|
|
|