🎨 优化鉴权中间件
This commit is contained in:
@@ -3,9 +3,11 @@ package middleware
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"git.echol.cn/loser/lckt/global"
|
"git.echol.cn/loser/lckt/global"
|
||||||
|
"git.echol.cn/loser/lckt/model/user"
|
||||||
"git.echol.cn/loser/lckt/utils"
|
"git.echol.cn/loser/lckt/utils"
|
||||||
"git.echol.cn/loser/lckt/utils/user_jwt"
|
"git.echol.cn/loser/lckt/utils/user_jwt"
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
|
"go.uber.org/zap"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -23,7 +25,7 @@ func UserJWTAuth() gin.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
j := utils.NewJWT()
|
j := user_jwt.NewUserJWT()
|
||||||
// parseToken 解析token包含的信息
|
// parseToken 解析token包含的信息
|
||||||
claims, err := j.ParseToken(token)
|
claims, err := j.ParseToken(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -39,6 +41,23 @@ func UserJWTAuth() gin.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 查询用户是否被禁用
|
||||||
|
status := 1
|
||||||
|
err = global.GVA_DB.Model(&user.User{}).Where("id = ?", claims.BaseClaims.ID).Select("status").Scan(&status).Error
|
||||||
|
if err != nil {
|
||||||
|
global.GVA_LOG.Error("中间件查询用户状态失败", zap.Error(err))
|
||||||
|
response.FailWithMessage(err.Error(), c)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if status == 0 {
|
||||||
|
response.Banned("用户已被禁用", c)
|
||||||
|
user_jwt.ClearToken(c)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.Set("claims", claims)
|
c.Set("claims", claims)
|
||||||
if claims.ExpiresAt.Unix()-time.Now().Unix() < claims.BufferTime {
|
if claims.ExpiresAt.Unix()-time.Now().Unix() < claims.BufferTime {
|
||||||
dr, _ := utils.ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime)
|
dr, _ := utils.ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime)
|
||||||
@@ -50,7 +69,7 @@ func UserJWTAuth() gin.HandlerFunc {
|
|||||||
user_jwt.SetToken(c, newToken, int(dr.Seconds()))
|
user_jwt.SetToken(c, newToken, int(dr.Seconds()))
|
||||||
if global.GVA_CONFIG.System.UseMultipoint {
|
if global.GVA_CONFIG.System.UseMultipoint {
|
||||||
// 记录新的活跃jwt
|
// 记录新的活跃jwt
|
||||||
_ = utils.SetRedisJWT(newToken, newClaims.Username)
|
_ = utils.SetRedisJWT(newToken, newClaims.NickName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.Next()
|
c.Next()
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
package vo
|
package vo
|
||||||
|
|
||||||
|
import "git.echol.cn/loser/lckt/model/app"
|
||||||
|
|
||||||
type UserInfo struct {
|
type UserInfo struct {
|
||||||
ID uint `json:"id" form:"id"`
|
ID uint `json:"id" form:"id"`
|
||||||
NickName string `json:"nick_name" form:"nick_name"`
|
NickName string `json:"nick_name" form:"nick_name"`
|
||||||
@@ -30,3 +32,11 @@ type TeacherVipInfo struct {
|
|||||||
Title string `json:"title" form:"title"`
|
Title string `json:"title" form:"title"`
|
||||||
ExpireAt string `json:"expire_at" form:"expire_at"`
|
ExpireAt string `json:"expire_at" form:"expire_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TeacherVipList struct {
|
||||||
|
app.TeacherVip
|
||||||
|
IsBuy int `json:"is_buy" form:"is_buy"` //是否购买 0 否 1 是
|
||||||
|
ExpireAt string `json:"expire_at" form:"expire_at"`
|
||||||
|
//是否过期
|
||||||
|
IsExpire int `json:"is_expire" form:"is_expire"` //是否过期 1 未过期 2 已过期
|
||||||
|
}
|
||||||
|
@@ -6,6 +6,7 @@ type BaseClaims struct {
|
|||||||
NickName string `json:"nickName"`
|
NickName string `json:"nickName"`
|
||||||
ID uint `json:"id"`
|
ID uint `json:"id"`
|
||||||
Phone string `json:"phone"`
|
Phone string `json:"phone"`
|
||||||
|
Status int8 `json:"status"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CustomClaims struct {
|
type CustomClaims struct {
|
||||||
|
@@ -94,6 +94,7 @@ func LoginToken(user user.User) (token string, claims request.CustomClaims, err
|
|||||||
ID: user.ID,
|
ID: user.ID,
|
||||||
NickName: user.NickName,
|
NickName: user.NickName,
|
||||||
Phone: user.Phone,
|
Phone: user.Phone,
|
||||||
|
Status: user.Status,
|
||||||
})
|
})
|
||||||
token, err = j.CreateToken(claims)
|
token, err = j.CreateToken(claims)
|
||||||
return
|
return
|
||||||
|
Reference in New Issue
Block a user