提交 1b0e8fec authored 作者: mooncake's avatar mooncake

modify middleware auth

上级 6c43e368
...@@ -76,17 +76,11 @@ func Auth(opts ...JwtOption) gin.HandlerFunc { ...@@ -76,17 +76,11 @@ func Auth(opts ...JwtOption) gin.HandlerFunc {
c.Abort() c.Abort()
return return
} }
// if len(authorization) < 150 {
// logger.Warn("authorization is illegal")
// responseUnauthorized(c, o.isSwitchHTTPCode)
// c.Abort()
// return
// }
token := authorization[7:] // remove Bearer prefix token := authorization[7:] // remove Bearer prefix
claims, err := jwt.ParseToken(token) claims, err := jwt.ParseToken(token)
if err != nil { if err != nil {
logger.Warn("ParseToken error", logger.Err(err)) logger.Error("parseToken error", logger.Err(err), ctxutils.GinTraceIDField(c))
responseUnauthorized(c, o.isSwitchHTTPCode) responseUnauthorized(c, o.isSwitchHTTPCode)
c.Abort() c.Abort()
return return
...@@ -95,7 +89,7 @@ func Auth(opts ...JwtOption) gin.HandlerFunc { ...@@ -95,7 +89,7 @@ func Auth(opts ...JwtOption) gin.HandlerFunc {
if o.verify != nil { if o.verify != nil {
tokenTail10 := token[len(token)-10:] tokenTail10 := token[len(token)-10:]
if err = o.verify(claims, tokenTail10, c); err != nil { if err = o.verify(claims, tokenTail10, c); err != nil {
logger.Warn("verify error", logger.Err(err), logger.String("uid", claims.UID), logger.String("name", claims.Name)) logger.Error("verify token error", logger.Err(err), logger.Any("claims", claims), ctxutils.GinTraceIDField(c))
responseUnauthorized(c, o.isSwitchHTTPCode) responseUnauthorized(c, o.isSwitchHTTPCode)
c.Abort() c.Abort()
return return
...@@ -151,11 +145,11 @@ func AuthCustom(verify VerifyCustomFn, opts ...JwtOption) gin.HandlerFunc { ...@@ -151,11 +145,11 @@ func AuthCustom(verify VerifyCustomFn, opts ...JwtOption) gin.HandlerFunc {
c.Abort() c.Abort()
return return
} }
// token := strings.TrimPrefix(authorization, "Bearer ")
token := authorization[7:] // remove Bearer prefix token := authorization[7:] // remove Bearer prefix
claims, err := jwt.ParseCustomToken(token) claims, err := jwt.ParseCustomToken(token)
if err != nil { if err != nil {
logger.Warn("ParseToken error", logger.Err(err)) logger.Error("parseToken error", logger.Err(err), ctxutils.GinTraceIDField(c))
responseUnauthorized(c, o.isSwitchHTTPCode) responseUnauthorized(c, o.isSwitchHTTPCode)
c.Abort() c.Abort()
return return
...@@ -163,7 +157,7 @@ func AuthCustom(verify VerifyCustomFn, opts ...JwtOption) gin.HandlerFunc { ...@@ -163,7 +157,7 @@ func AuthCustom(verify VerifyCustomFn, opts ...JwtOption) gin.HandlerFunc {
tokenTail10 := token[len(token)-10:] tokenTail10 := token[len(token)-10:]
if err = verify(claims, tokenTail10, c); err != nil { if err = verify(claims, tokenTail10, c); err != nil {
logger.Warn("verify error", logger.Err(err), logger.Any("fields", claims.Fields)) logger.Error("verify token error", logger.Err(err), logger.Any("fields", claims.Fields), ctxutils.GinTraceIDField(c))
responseUnauthorized(c, o.isSwitchHTTPCode) responseUnauthorized(c, o.isSwitchHTTPCode)
c.Abort() c.Abort()
return return
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论