mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-08-28 05:12:32 +08:00
修复微信登录ak本地缓存刷新问题
This commit is contained in:
@@ -14,30 +14,30 @@ import (
|
||||
"hotgo/internal/library/contexts"
|
||||
"hotgo/internal/library/response"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/simple"
|
||||
)
|
||||
|
||||
// AdminAuth 后台鉴权中间件
|
||||
func (s *sMiddleware) AdminAuth(r *ghttp.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
prefix = g.Cfg().MustGet(ctx, "router.admin.prefix", "/admin").String()
|
||||
path = gstr.Replace(r.URL.Path, prefix, "", 1)
|
||||
ctx = r.Context()
|
||||
path = gstr.Replace(r.URL.Path, simple.RouterPrefix(ctx, consts.AppAdmin), "", 1)
|
||||
)
|
||||
|
||||
// 不需要验证登录的路由地址
|
||||
if isExceptLogin(ctx, consts.AppAdmin, path) {
|
||||
if s.IsExceptLogin(ctx, consts.AppAdmin, path) {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// 将用户信息传递到上下文中
|
||||
if err := deliverUserContext(r); err != nil {
|
||||
if err := s.DeliverUserContext(r); err != nil {
|
||||
response.JsonExit(r, gcode.CodeNotAuthorized.Code(), err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 不需要验证权限的路由地址
|
||||
if isExceptAuth(ctx, consts.AppAdmin, path) {
|
||||
if s.IsExceptAuth(ctx, consts.AppAdmin, path) {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
|
@@ -7,29 +7,28 @@ package middleware
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/library/response"
|
||||
"hotgo/utility/simple"
|
||||
)
|
||||
|
||||
// ApiAuth API鉴权中间件
|
||||
func (s *sMiddleware) ApiAuth(r *ghttp.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
prefix = g.Cfg().MustGet(ctx, "router.api.prefix", "/api").String()
|
||||
path = gstr.Replace(r.URL.Path, prefix, "", 1)
|
||||
ctx = r.Context()
|
||||
path = gstr.Replace(r.URL.Path, simple.RouterPrefix(ctx, consts.AppApi), "", 1)
|
||||
)
|
||||
|
||||
// 不需要验证登录的路由地址
|
||||
if isExceptLogin(ctx, consts.AppApi, path) {
|
||||
if s.IsExceptLogin(ctx, consts.AppApi, path) {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// 将用户信息传递到上下文中
|
||||
if err := deliverUserContext(r); err != nil {
|
||||
if err := s.DeliverUserContext(r); err != nil {
|
||||
response.JsonExit(r, gcode.CodeNotAuthorized.Code(), err.Error())
|
||||
return
|
||||
}
|
||||
|
@@ -12,8 +12,12 @@ import (
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/net/gtrace"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/global"
|
||||
"hotgo/internal/library/addons"
|
||||
"hotgo/internal/library/contexts"
|
||||
"hotgo/internal/library/response"
|
||||
@@ -53,6 +57,16 @@ func NewMiddleware() *sMiddleware {
|
||||
|
||||
// Ctx 初始化请求上下文
|
||||
func (s *sMiddleware) Ctx(r *ghttp.Request) {
|
||||
if global.JaegerSwitch {
|
||||
ctx, span := gtrace.NewSpan(r.Context(), "middleware.ctx")
|
||||
span.SetAttributes(attribute.KeyValue{
|
||||
Key: "traceID",
|
||||
Value: attribute.StringValue(gctx.CtxId(ctx)),
|
||||
})
|
||||
span.End()
|
||||
r.SetCtx(ctx)
|
||||
}
|
||||
|
||||
contexts.Init(r, &model.Context{
|
||||
Data: make(g.Map),
|
||||
Module: getModule(r.URL.Path),
|
||||
@@ -73,7 +87,6 @@ func getModule(path string) (module string) {
|
||||
module = consts.AppDefault
|
||||
return
|
||||
}
|
||||
|
||||
return slice[1]
|
||||
}
|
||||
|
||||
@@ -124,8 +137,8 @@ func (s *sMiddleware) Addon(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
// deliverUserContext 将用户信息传递到上下文中
|
||||
func deliverUserContext(r *ghttp.Request) (err error) {
|
||||
// DeliverUserContext 将用户信息传递到上下文中
|
||||
func (s *sMiddleware) DeliverUserContext(r *ghttp.Request) (err error) {
|
||||
user, err := token.ParseLoginUser(r)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -134,8 +147,8 @@ func deliverUserContext(r *ghttp.Request) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// isExceptAuth 是否是不需要验证权限的路由地址
|
||||
func isExceptAuth(ctx context.Context, appName, path string) bool {
|
||||
// IsExceptAuth 是否是不需要验证权限的路由地址
|
||||
func (s *sMiddleware) IsExceptAuth(ctx context.Context, appName, path string) bool {
|
||||
pathList := g.Cfg().MustGet(ctx, fmt.Sprintf("router.%v.exceptAuth", appName)).Strings()
|
||||
|
||||
for i := 0; i < len(pathList); i++ {
|
||||
@@ -143,12 +156,11 @@ func isExceptAuth(ctx context.Context, appName, path string) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// isExceptLogin 是否是不需要登录的路由地址
|
||||
func isExceptLogin(ctx context.Context, appName, path string) bool {
|
||||
// IsExceptLogin 是否是不需要登录的路由地址
|
||||
func (s *sMiddleware) IsExceptLogin(ctx context.Context, appName, path string) bool {
|
||||
pathList := g.Cfg().MustGet(ctx, fmt.Sprintf("router.%v.exceptLogin", appName)).Strings()
|
||||
|
||||
for i := 0; i < len(pathList); i++ {
|
||||
@@ -156,6 +168,5 @@ func isExceptLogin(ctx context.Context, appName, path string) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
@@ -7,29 +7,28 @@ package middleware
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/library/response"
|
||||
"hotgo/utility/simple"
|
||||
)
|
||||
|
||||
// WebSocketAuth websocket鉴权中间件
|
||||
func (s *sMiddleware) WebSocketAuth(r *ghttp.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
prefix = g.Cfg().MustGet(ctx, "router.websocket.prefix", "/websocket").String()
|
||||
path = gstr.Replace(r.URL.Path, prefix, "", 1)
|
||||
ctx = r.Context()
|
||||
path = gstr.Replace(r.URL.Path, simple.RouterPrefix(ctx, consts.AppWebSocket), "", 1)
|
||||
)
|
||||
|
||||
// 不需要验证登录的路由地址
|
||||
if isExceptLogin(ctx, consts.AppWebSocket, path) {
|
||||
if s.IsExceptLogin(ctx, consts.AppWebSocket, path) {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// 将用户信息传递到上下文中
|
||||
if err := deliverUserContext(r); err != nil {
|
||||
if err := s.DeliverUserContext(r); err != nil {
|
||||
response.JsonExit(r, gcode.CodeNotAuthorized.Code(), err.Error())
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user