2025-04-30 15:46:14 +08:00
|
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"admin/internal/context"
|
|
|
|
|
"admin/internal/errcode"
|
2025-05-16 17:29:05 +08:00
|
|
|
|
"admin/lib/xlog"
|
2025-04-30 15:46:14 +08:00
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (srv *Server) CheckToken(ctx *context.WebContext) {
|
2025-05-16 17:29:05 +08:00
|
|
|
|
reqPath := ctx.GinCtx().Request.URL.Path
|
|
|
|
|
xlog.Tracef("请求路径:%v, 头:%+v", reqPath, ctx.GinCtx().Request.Header)
|
|
|
|
|
if strings.Contains(reqPath, "/login") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if len(reqPath) > 3 && reqPath[:4] != "/api" { // 用来过滤出非/api请求,前后端部署到一起时,访问静态页面的请求跳过token检查
|
|
|
|
|
ctx.GinCtx().Next()
|
2025-04-30 15:46:14 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err := ctx.ExtractHeader()
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.Fail(errcode.New(errcode.HeaderParamsInvalid, "header invalid"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = srv.svc.CheckToken(ctx.Header.Token, ctx.Header.UserId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.Fail(err)
|
|
|
|
|
ctx.GinCtx().Abort()
|
|
|
|
|
} else {
|
|
|
|
|
ctx.GinCtx().Next()
|
|
|
|
|
}
|
|
|
|
|
}
|