feat: 修改jwt逻辑,登出黑名单处理
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
package utils
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strings"
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/model/system"
|
||||
systemReq "sundynix-go/model/system/request"
|
||||
@@ -20,33 +21,7 @@ func GetLoginToken(user system.Login) (token string, claims systemReq.CustomClai
|
||||
return
|
||||
}
|
||||
|
||||
// SetToken 设置一个名为 "x-token" 的 Cookie,并根据请求的主机名和 IP 地址来设置 Cookie 的域。
|
||||
//
|
||||
// 参数:
|
||||
// - c: *gin.Context, Gin 框架的上下文对象,用于处理 HTTP 请求和响应。
|
||||
// - token: string, 要设置的 token 值。
|
||||
// - maxAge: int, Cookie 的最大生存时间(以秒为单位)。
|
||||
//
|
||||
// 该函数首先从请求的主机名中提取出主机部分(去除端口号),然后判断该主机名是否为 IP 地址。
|
||||
// 如果是 IP 地址,则设置 Cookie 的域为空;否则,将 Cookie 的域设置为提取出的主机名。
|
||||
func SetToken(c *gin.Context, token string, maxAge int) {
|
||||
// 从请求的主机名中提取主机部分,忽略端口号
|
||||
host, _, err := net.SplitHostPort(c.Request.Host)
|
||||
if err != nil {
|
||||
host = c.Request.Host
|
||||
}
|
||||
|
||||
// 判断主机名是否为 IP 地址,并根据结果设置 Cookie 的域
|
||||
if net.ParseIP(host) != nil {
|
||||
c.SetCookie("x-token", token, maxAge, "/", "", false, false)
|
||||
} else {
|
||||
c.SetCookie("x-token", token, maxAge, "/", host, false, false)
|
||||
}
|
||||
}
|
||||
|
||||
// GetToken 从请求头或Cookie中获取JWT token,并确保其有效性。
|
||||
// 如果请求头中没有Authorization字段,则尝试从Cookie中获取token,并解析验证其有效性。
|
||||
// 如果token有效,则将其重新写入Cookie,并设置过期时间。
|
||||
// GetToken 从请求头中获取JWT token,并确保其有效性
|
||||
//
|
||||
// 参数:
|
||||
// - c: *gin.Context, Gin框架的上下文对象,用于获取请求信息和设置响应。
|
||||
@@ -56,24 +31,10 @@ func SetToken(c *gin.Context, token string, maxAge int) {
|
||||
func GetToken(c *gin.Context) string {
|
||||
// 从请求头中获取Authorization字段的值
|
||||
token := c.Request.Header.Get("Authorization")
|
||||
|
||||
//// 如果请求头中没有Authorization字段,则尝试从Cookie中获取token
|
||||
//if token == "" {
|
||||
// j := NewJWT()
|
||||
// token, _ = c.Cookie("x-token")
|
||||
//
|
||||
// // 解析并验证token的有效性
|
||||
// claims, err := j.ParseToken(token)
|
||||
// if err != nil {
|
||||
// // 如果解析失败,记录错误日志并返回当前token
|
||||
// global.Logger.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在x-token且claims是否为规定结构")
|
||||
// return token
|
||||
// }
|
||||
//
|
||||
// // 如果token有效,则将其重新写入Cookie,并设置过期时间
|
||||
// SetToken(c, token, int((claims.ExpiresAt.Unix()-time.Now().Unix())/60))
|
||||
//}
|
||||
|
||||
prefix := strings.HasPrefix(token, "Bearer ")
|
||||
if prefix {
|
||||
token = strings.TrimPrefix(token, "Bearer ")
|
||||
}
|
||||
// 返回获取到的token
|
||||
return token
|
||||
}
|
||||
@@ -85,9 +46,9 @@ func ClearToken(c *gin.Context) {
|
||||
host = c.Request.Host
|
||||
}
|
||||
if net.ParseIP(host) != nil {
|
||||
c.SetCookie("x-token", "", -1, "/", "", false, false)
|
||||
c.SetCookie("sundynix-token", "", -1, "/", "", false, false)
|
||||
} else {
|
||||
c.SetCookie("x-token", "", -1, "/", host, false, false)
|
||||
c.SetCookie("sundynix-token", "", -1, "/", host, false, false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package utils
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/model/system/request"
|
||||
"sundynix-go/utils"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
@@ -31,8 +32,8 @@ func NewJWT() *JWT {
|
||||
|
||||
// CreateClaims 创建Claims
|
||||
func (j *JWT) CreateClaims(baseClaims request.BaseClaims) request.CustomClaims {
|
||||
bf, _ := ParseDuration(global.Config.JWT.BufferTime)
|
||||
ep, _ := ParseDuration(global.Config.JWT.ExpiresTime)
|
||||
bf, _ := utils.ParseDuration(global.Config.JWT.BufferTime)
|
||||
ep, _ := utils.ParseDuration(global.Config.JWT.ExpiresTime)
|
||||
claims := request.CustomClaims{
|
||||
BaseClaims: baseClaims,
|
||||
BufferTime: int64(bf / time.Second), // 缓冲时间1天 缓冲时间内会获得新的token刷新令牌 此时一个用户会存在两个有效令牌 但是前端只留一个 另一个会丢失
|
||||
Reference in New Issue
Block a user