This commit is contained in:
henry
2021-11-24 09:59:29 +08:00
parent cf91d55ab2
commit f007168919
21 changed files with 360 additions and 93 deletions

View File

@ -4,13 +4,9 @@ import (
"SciencesServer/app/service"
"SciencesServer/config"
"SciencesServer/serve/cache"
cache2 "SciencesServer/serve/cache"
"SciencesServer/utils"
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"net/http"
)
// SkipperURL 跳过验证
@ -41,34 +37,10 @@ func NeedLogin(skipperURL ...SkipperURL) gin.HandlerFunc {
c.Abort()
return
}
tokenInfo := utils.JWTDecrypt(token)
session, err := service.NewAuthToken(token).Auth()
if tokenInfo == nil || len(tokenInfo) <= 0 {
c.JSON(http.StatusUnauthorized, gin.H{"message": "Token无效"})
c.Abort()
return
}
expTimestamp := utils.StringToInt64(fmt.Sprintf("%v", tokenInfo["exp"]))
expTime := time.Unix(expTimestamp, 0)
ok := expTime.After(time.Now())
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"message": "Token过期"})
c.Abort()
return
}
cache, _ := cache2.Cache.HGet(config.RedisKeyForAccount, fmt.Sprintf("%v", tokenInfo[config.TokenForUID]))
if cache == "" {
c.JSON(http.StatusUnauthorized, gin.H{"message": "用户未登录或已退出"})
c.Abort()
return
}
session := new(service.Session)
_ = session.UnmarshalBinary([]byte(cache))
if !config.SettingInfo.MultipleLogin && session.Token != token {
c.JSON(http.StatusUnauthorized, gin.H{"message": "登录失效,已在其他地方登录!"})
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"message": err.Error()})
c.Abort()
return
}