From ee1a42a10660babbb942abc6ad4dd47a27ab2fbc Mon Sep 17 00:00:00 2001 From: bypanghu Date: Mon, 25 Nov 2024 15:19:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E5=8C=85=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/const/const.go | 2 +- server/const/errors.go | 2 +- server/middleware/email.go | 3 ++- server/middleware/jwt.go | 16 ++++++++-------- server/middleware/limit_ip.go | 4 ++-- server/middleware/operation.go | 3 ++- server/model/common/response/response.go | 18 +++++++++--------- server/utils/claims.go | 24 ++++++++++++------------ 8 files changed, 37 insertions(+), 35 deletions(-) diff --git a/server/const/const.go b/server/const/const.go index 54603dbbe0..d5e66108dd 100644 --- a/server/const/const.go +++ b/server/const/const.go @@ -1,4 +1,4 @@ -package _const +package constant // gva 常量文件,用于定义一些常量 , 如果修改此处,请自行修改前端中的/src/const/index.js 中对应的 const TOKEN_NAME = "x-token" // token ,此处是存储在localStorage中的token名称 diff --git a/server/const/errors.go b/server/const/errors.go index c90456fcbb..c928ecf225 100644 --- a/server/const/errors.go +++ b/server/const/errors.go @@ -1,4 +1,4 @@ -package _const +package constant const ( JWT_NO_AUTH = 6 diff --git a/server/middleware/email.go b/server/middleware/email.go index 4a07561c98..f33eb0bbf3 100644 --- a/server/middleware/email.go +++ b/server/middleware/email.go @@ -2,6 +2,7 @@ package middleware import ( "bytes" + constant "github.com/flipped-aurora/gin-vue-admin/server/const" "io" "strconv" "time" @@ -25,7 +26,7 @@ func ErrorToEmail() gin.HandlerFunc { if claims.Username != "" { username = claims.Username } else { - id, _ := strconv.Atoi(c.Request.Header.Get("x-user-id")) + id, _ := strconv.Atoi(c.Request.Header.Get(constant.REQUEST_USER_ID)) user, err := userService.FindUserById(id) if err != nil { username = "Unknown" diff --git a/server/middleware/jwt.go b/server/middleware/jwt.go index af58579216..eb21bc1db6 100644 --- a/server/middleware/jwt.go +++ b/server/middleware/jwt.go @@ -2,7 +2,7 @@ package middleware import ( "errors" - _const "github.com/flipped-aurora/gin-vue-admin/server/const" + "github.com/flipped-aurora/gin-vue-admin/server/const" "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/utils" "github.com/golang-jwt/jwt/v4" @@ -56,14 +56,14 @@ func JWTAuth() gin.HandlerFunc { // response.FailWithDetailed(gin.H{"reload": true}, err.Error(), c) // c.Abort() //} - c.Set(_const.JWT_CLAIMS, claims) + c.Set(constant.JWT_CLAIMS, claims) if claims.ExpiresAt.Unix()-time.Now().Unix() < claims.BufferTime { dr, _ := utils.ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime) claims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(dr)) newToken, _ := j.CreateTokenByOldToken(token, *claims) newClaims, _ := j.ParseToken(newToken) - c.Header(_const.NEW_TOKEN_NAME, newToken) - c.Header(_const.NEW_TOKEN_EXPIRES_AT, strconv.FormatInt(newClaims.ExpiresAt.Unix(), 10)) + c.Header(constant.NEW_TOKEN_NAME, newToken) + c.Header(constant.NEW_TOKEN_EXPIRES_AT, strconv.FormatInt(newClaims.ExpiresAt.Unix(), 10)) utils.SetToken(c, newToken, int(dr.Seconds())) if global.GVA_CONFIG.System.UseMultipoint { // 记录新的活跃jwt @@ -72,11 +72,11 @@ func JWTAuth() gin.HandlerFunc { } c.Next() - if newToken, exists := c.Get(_const.NEW_TOKEN_NAME); exists { - c.Header(_const.NEW_TOKEN_NAME, newToken.(string)) + if newToken, exists := c.Get(constant.NEW_TOKEN_NAME); exists { + c.Header(constant.NEW_TOKEN_NAME, newToken.(string)) } - if newExpiresAt, exists := c.Get(_const.NEW_TOKEN_EXPIRES_AT); exists { - c.Header(_const.NEW_TOKEN_EXPIRES_AT, newExpiresAt.(string)) + if newExpiresAt, exists := c.Get(constant.NEW_TOKEN_EXPIRES_AT); exists { + c.Header(constant.NEW_TOKEN_EXPIRES_AT, newExpiresAt.(string)) } } } diff --git a/server/middleware/limit_ip.go b/server/middleware/limit_ip.go index 9f63a7d61a..c250905f09 100644 --- a/server/middleware/limit_ip.go +++ b/server/middleware/limit_ip.go @@ -3,7 +3,7 @@ package middleware import ( "context" "errors" - _const "github.com/flipped-aurora/gin-vue-admin/server/const" + "github.com/flipped-aurora/gin-vue-admin/server/const" "net/http" "time" @@ -27,7 +27,7 @@ type LimitConfig struct { func (l LimitConfig) LimitWithTime() gin.HandlerFunc { return func(c *gin.Context) { if err := l.CheckOrMark(l.GenerationKey(c), l.Expire, l.Limit); err != nil { - c.JSON(http.StatusOK, gin.H{"code": _const.ERROR, "msg": err}) + c.JSON(http.StatusOK, gin.H{"code": constant.ERROR, "msg": err}) c.Abort() return } else { diff --git a/server/middleware/operation.go b/server/middleware/operation.go index f34cf68ee2..2cb214e568 100644 --- a/server/middleware/operation.go +++ b/server/middleware/operation.go @@ -3,6 +3,7 @@ package middleware import ( "bytes" "encoding/json" + constant "github.com/flipped-aurora/gin-vue-admin/server/const" "io" "net/http" "net/url" @@ -60,7 +61,7 @@ func OperationRecord() gin.HandlerFunc { if claims != nil && claims.BaseClaims.ID != 0 { userId = int(claims.BaseClaims.ID) } else { - id, err := strconv.Atoi(c.Request.Header.Get("x-user-id")) + id, err := strconv.Atoi(c.Request.Header.Get(constant.REQUEST_USER_ID)) if err != nil { userId = 0 } diff --git a/server/model/common/response/response.go b/server/model/common/response/response.go index 51b4bca408..561a9e7f5d 100644 --- a/server/model/common/response/response.go +++ b/server/model/common/response/response.go @@ -1,7 +1,7 @@ package response import ( - _const "github.com/flipped-aurora/gin-vue-admin/server/const" + "github.com/flipped-aurora/gin-vue-admin/server/const" "net/http" "github.com/gin-gonic/gin" @@ -23,37 +23,37 @@ func Result(code int, data interface{}, msg string, c *gin.Context) { } func Ok(c *gin.Context) { - Result(_const.SUCCESS, map[string]interface{}{}, "操作成功", c) + Result(constant.SUCCESS, map[string]interface{}{}, "操作成功", c) } func OkWithMessage(message string, c *gin.Context) { - Result(_const.SUCCESS, map[string]interface{}{}, message, c) + Result(constant.SUCCESS, map[string]interface{}{}, message, c) } func OkWithData(data interface{}, c *gin.Context) { - Result(_const.SUCCESS, data, "成功", c) + Result(constant.SUCCESS, data, "成功", c) } func OkWithDetailed(data interface{}, message string, c *gin.Context) { - Result(_const.SUCCESS, data, message, c) + Result(constant.SUCCESS, data, message, c) } func Fail(c *gin.Context) { - Result(_const.ERROR, map[string]interface{}{}, "操作失败", c) + Result(constant.ERROR, map[string]interface{}{}, "操作失败", c) } func FailWithMessage(message string, c *gin.Context) { - Result(_const.ERROR, map[string]interface{}{}, message, c) + Result(constant.ERROR, map[string]interface{}{}, message, c) } func NoAuth(message string, c *gin.Context) { c.JSON(http.StatusUnauthorized, Response{ - _const.JWT_NO_AUTH, + constant.JWT_NO_AUTH, nil, message, }) } func FailWithDetailed(data interface{}, message string, c *gin.Context) { - Result(_const.ERROR, data, message, c) + Result(constant.ERROR, data, message, c) } diff --git a/server/utils/claims.go b/server/utils/claims.go index 0129526df6..7a1084b36f 100644 --- a/server/utils/claims.go +++ b/server/utils/claims.go @@ -1,7 +1,7 @@ package utils import ( - _const "github.com/flipped-aurora/gin-vue-admin/server/const" + "github.com/flipped-aurora/gin-vue-admin/server/const" "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/model/system" systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request" @@ -19,9 +19,9 @@ func ClearToken(c *gin.Context) { } if net.ParseIP(host) != nil { - c.SetCookie(_const.REQUSET_COOKIE_TOKEN_NAME, "", -1, "/", "", false, false) + c.SetCookie(constant.REQUSET_COOKIE_TOKEN_NAME, "", -1, "/", "", false, false) } else { - c.SetCookie(_const.REQUSET_COOKIE_TOKEN_NAME, "", -1, "/", host, false, false) + c.SetCookie(constant.REQUSET_COOKIE_TOKEN_NAME, "", -1, "/", host, false, false) } } @@ -33,17 +33,17 @@ func SetToken(c *gin.Context, token string, maxAge int) { } if net.ParseIP(host) != nil { - c.SetCookie(_const.REQUSET_COOKIE_TOKEN_NAME, token, maxAge, "/", "", false, false) + c.SetCookie(constant.REQUSET_COOKIE_TOKEN_NAME, token, maxAge, "/", "", false, false) } else { - c.SetCookie(_const.REQUSET_COOKIE_TOKEN_NAME, token, maxAge, "/", host, false, false) + c.SetCookie(constant.REQUSET_COOKIE_TOKEN_NAME, token, maxAge, "/", host, false, false) } } func GetToken(c *gin.Context) string { - token, _ := c.Cookie(_const.REQUSET_COOKIE_TOKEN_NAME) + token, _ := c.Cookie(constant.REQUSET_COOKIE_TOKEN_NAME) if token == "" { j := NewJWT() - token = c.Request.Header.Get(_const.REQUEST_HEAER_TOKEN_NAME) + token = c.Request.Header.Get(constant.REQUEST_HEAER_TOKEN_NAME) claims, err := j.ParseToken(token) if err != nil { global.GVA_LOG.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在x-token且claims是否为规定结构") @@ -66,7 +66,7 @@ func GetClaims(c *gin.Context) (*systemReq.CustomClaims, error) { // GetUserID 从Gin的Context中获取从jwt解析出来的用户ID func GetUserID(c *gin.Context) uint { - if claims, exists := c.Get(_const.JWT_CLAIMS); !exists { + if claims, exists := c.Get(constant.JWT_CLAIMS); !exists { if cl, err := GetClaims(c); err != nil { return 0 } else { @@ -80,7 +80,7 @@ func GetUserID(c *gin.Context) uint { // GetUserUuid 从Gin的Context中获取从jwt解析出来的用户UUID func GetUserUuid(c *gin.Context) uuid.UUID { - if claims, exists := c.Get(_const.JWT_CLAIMS); !exists { + if claims, exists := c.Get(constant.JWT_CLAIMS); !exists { if cl, err := GetClaims(c); err != nil { return uuid.UUID{} } else { @@ -94,7 +94,7 @@ func GetUserUuid(c *gin.Context) uuid.UUID { // GetUserAuthorityId 从Gin的Context中获取从jwt解析出来的用户角色id func GetUserAuthorityId(c *gin.Context) uint { - if claims, exists := c.Get(_const.JWT_CLAIMS); !exists { + if claims, exists := c.Get(constant.JWT_CLAIMS); !exists { if cl, err := GetClaims(c); err != nil { return 0 } else { @@ -108,7 +108,7 @@ func GetUserAuthorityId(c *gin.Context) uint { // GetUserInfo 从Gin的Context中获取从jwt解析出来的用户角色id func GetUserInfo(c *gin.Context) *systemReq.CustomClaims { - if claims, exists := c.Get(_const.JWT_CLAIMS); !exists { + if claims, exists := c.Get(constant.JWT_CLAIMS); !exists { if cl, err := GetClaims(c); err != nil { return nil } else { @@ -122,7 +122,7 @@ func GetUserInfo(c *gin.Context) *systemReq.CustomClaims { // GetUserName 从Gin的Context中获取从jwt解析出来的用户名 func GetUserName(c *gin.Context) string { - if claims, exists := c.Get(_const.JWT_CLAIMS); !exists { + if claims, exists := c.Get(constant.JWT_CLAIMS); !exists { if cl, err := GetClaims(c); err != nil { return "" } else {