This commit is contained in:
JiXieShi
2024-11-16 23:59:15 +08:00
parent f722153536
commit 87859c7bb8
42 changed files with 2018 additions and 485 deletions

View File

@@ -12,11 +12,11 @@ func JWTAuth(config *utils.JWTConfig) gin.HandlerFunc {
return func(c *gin.Context) {
var token string
// 1. 首先从 cookie 中获取 token
tokenCookie, err := c.Cookie("token")
if err == nil {
token = tokenCookie
}
// // 1. 首先从 cookie 中获取 token
// tokenCookie, err := c.Cookie("token")
// if err == nil {
// token = tokenCookie
// }
// 2. 如果 cookie 中没有,则从 header 中获取
if token == "" {
@@ -44,8 +44,6 @@ func JWTAuth(config *utils.JWTConfig) gin.HandlerFunc {
// 验证 token
claims, err := utils.ParseToken(token, config)
if err != nil {
// 如果 token 无效,清除 cookie
// c.SetCookie("token", "", -1, "/", "", true, true)
c.JSON(http.StatusUnauthorized, gin.H{"error": "无效的token"})
c.Abort()
return
@@ -56,12 +54,6 @@ func JWTAuth(config *utils.JWTConfig) gin.HandlerFunc {
c.Set("username", claims.Username)
c.Set("role", claims.Role)
// 如果是从 header 或 query 参数获取的 token设置到 cookie 中
if tokenCookie == "" {
// 设置 cookie过期时间与 token 一致
// c.SetCookie("token", token, int(claims.ExpiresAt.Unix()-claims.IssuedAt.Unix()), "/", "", false, true)
}
c.Next()
}
}

View File

@@ -0,0 +1 @@