This commit is contained in:
+12
-1
@@ -68,13 +68,24 @@ func SetUserToken(c *gin.Context, token string, maxAge int) {
|
||||
}
|
||||
|
||||
func GetUserToken(c *gin.Context) string {
|
||||
// 首先尝试从 user-token 头部获取
|
||||
token := c.Request.Header.Get("user-token")
|
||||
|
||||
// 如果没有,尝试从 Authorization Bearer 头部获取
|
||||
if token == "" {
|
||||
authHeader := c.Request.Header.Get("Authorization")
|
||||
if authHeader != "" && len(authHeader) > 7 && authHeader[:7] == "Bearer " {
|
||||
token = authHeader[7:] // 去掉 "Bearer " 前缀
|
||||
}
|
||||
}
|
||||
|
||||
// 如果还是没有,尝试从 cookie 获取
|
||||
if token == "" {
|
||||
j := NewJWT()
|
||||
token, _ = c.Cookie("user-token")
|
||||
claims, err := j.ParseToken(token)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在user-token且claims是否为规定结构")
|
||||
global.GVA_LOG.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在user-token或Authorization Bearer且claims是否为规定结构")
|
||||
return token
|
||||
}
|
||||
SetUserToken(c, token, int((claims.ExpiresAt.Unix()-time.Now().Unix())/60))
|
||||
|
||||
Reference in New Issue
Block a user