Compare commits
No commits in common. "1005e89e4fe72eb79cd35eebb7c3f344f371896d" and "fc72fd123df09eea7ac161e2b07b404bd9a7df89" have entirely different histories.
1005e89e4f
...
fc72fd123d
|
@ -4,20 +4,20 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
|
"git.apinb.com/bsm-sdk/core/cache/redis"
|
||||||
"git.apinb.com/bsm-sdk/core/crypto/encipher"
|
"git.apinb.com/bsm-sdk/core/crypto/encipher"
|
||||||
"git.apinb.com/bsm-sdk/core/errcode"
|
"git.apinb.com/bsm-sdk/core/errcode"
|
||||||
"git.apinb.com/bsm-sdk/core/types"
|
"git.apinb.com/bsm-sdk/core/types"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func JwtAuth(time_verify bool) gin.HandlerFunc {
|
func JwtAuth(redis *redis.RedisClient) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
// 从请求头中获取 Authorization
|
// 从请求头中获取 Authorization
|
||||||
authHeader := c.GetHeader("Authorization")
|
authHeader := c.GetHeader("Authorization")
|
||||||
if authHeader == "" {
|
if authHeader == "" {
|
||||||
log.Printf("获取token异常:%v\n", "Authorization header is required")
|
log.Println("获取token异常:", "Authorization header is required")
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header is required"})
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header is required"})
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
|
@ -25,22 +25,21 @@ func JwtAuth(time_verify bool) gin.HandlerFunc {
|
||||||
// 提取Token
|
// 提取Token
|
||||||
claims, err := encipher.ParseTokenAes(authHeader)
|
claims, err := encipher.ParseTokenAes(authHeader)
|
||||||
if err != nil || claims == nil {
|
if err != nil || claims == nil {
|
||||||
log.Printf("提取token异常:%v\n", err)
|
log.Println("提取token异常:", "Token is required")
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token is required"})
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token is required"})
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检测是否需要验证token时间
|
// 从redis 获取token,判断当前redis 是否为空
|
||||||
if time_verify {
|
// tokenKey := fmt.Sprintf("%d-%s-%s", claims.ID, claims.Role, "token")
|
||||||
// 判断时间claims.ExpiresAt
|
// redisToken := redis.Client.Get(redis.Ctx, tokenKey)
|
||||||
if time.Now().Unix() > claims.ExpiresAt {
|
// if redisToken.Val() == "" {
|
||||||
log.Println("token过期,请重新获取:", "Token has expired")
|
// log.Println("redis异常", "Token status unauthorized")
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token has expired"})
|
// c.JSON(http.StatusUnauthorized, gin.H{"error": "Token status unauthorized"})
|
||||||
c.Abort()
|
// c.Abort()
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
// 将解析后的 Token 存储到上下文中
|
// 将解析后的 Token 存储到上下文中
|
||||||
c.Set("Auth", claims)
|
c.Set("Auth", claims)
|
||||||
|
|
Loading…
Reference in New Issue