im 管理后台
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
// Package middleware
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
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/contexts"
|
||||
"hotgo/internal/library/response"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/simple"
|
||||
)
|
||||
|
||||
// AdminAuth 后台鉴权中间件
|
||||
func (s *sMiddleware) AdminAuth(r *ghttp.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
path = gstr.Replace(r.URL.Path, simple.RouterPrefix(ctx, consts.AppAdmin), "", 1)
|
||||
)
|
||||
|
||||
// 不需要验证登录的路由地址
|
||||
if s.IsExceptLogin(ctx, consts.AppAdmin, path) {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// 将用户信息传递到上下文中
|
||||
if err := s.DeliverUserContext(r); err != nil {
|
||||
response.JsonExit(r, gcode.CodeNotAuthorized.Code(), err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 不需要验证权限的路由地址
|
||||
if s.IsExceptAuth(ctx, consts.AppAdmin, path) {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// 验证路由访问权限
|
||||
if !service.AdminRole().Verify(ctx, path, r.Method) {
|
||||
g.Log().Debugf(ctx, "AdminAuth fail path:%+v, GetRoleKey:%+v, r.Method:%+v", path, contexts.GetRoleKey(ctx), r.Method)
|
||||
response.JsonExit(r, gcode.CodeSecurityReason.Code(), "你没有访问权限!")
|
||||
return
|
||||
}
|
||||
|
||||
r.Middleware.Next()
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// Package middleware
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"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()
|
||||
path = gstr.Replace(r.URL.Path, simple.RouterPrefix(ctx, consts.AppApi), "", 1)
|
||||
)
|
||||
|
||||
// 不需要验证登录的路由地址
|
||||
if s.IsExceptLogin(ctx, consts.AppApi, path) {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// 将用户信息传递到上下文中
|
||||
if err := s.DeliverUserContext(r); err != nil {
|
||||
response.JsonExit(r, gcode.CodeNotAuthorized.Code(), err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 验证路由访问权限
|
||||
// ...
|
||||
|
||||
r.Middleware.Next()
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Package middleware
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
)
|
||||
|
||||
// HomeAuth 前台页面鉴权中间件
|
||||
func (s *sMiddleware) HomeAuth(r *ghttp.Request) {
|
||||
// 鉴权
|
||||
// ...
|
||||
|
||||
r.Middleware.Next()
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
// Package middleware
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2025 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/i18n/gi18n"
|
||||
"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/library/addons"
|
||||
"hotgo/internal/library/contexts"
|
||||
"hotgo/internal/library/response"
|
||||
"hotgo/internal/library/token"
|
||||
"hotgo/internal/model"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/simple"
|
||||
"hotgo/utility/validate"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type sMiddleware struct {
|
||||
LoginUrl string // 登录路由地址
|
||||
DemoWhiteList g.Map // 演示模式放行的路由白名单
|
||||
NotRecordRequest g.Map // 不记录请求数据的路由(当前请求数据过大时会影响响应效率,可以将路径放到该选项中改善)
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterMiddleware(NewMiddleware())
|
||||
}
|
||||
|
||||
func NewMiddleware() *sMiddleware {
|
||||
return &sMiddleware{
|
||||
LoginUrl: "/common",
|
||||
DemoWhiteList: g.Map{
|
||||
"/admin/site/accountLogin": struct{}{}, // 账号登录
|
||||
"/admin/site/mobileLogin": struct{}{}, // 手机号登录
|
||||
"/admin/genCodes/preview": struct{}{}, // 预览代码
|
||||
},
|
||||
NotRecordRequest: g.Map{
|
||||
"/admin/upload/file": struct{}{}, // 上传文件
|
||||
"/admin/upload/uploadPart": struct{}{}, // 上传分片
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Ctx 初始化请求上下文
|
||||
func (s *sMiddleware) Ctx(r *ghttp.Request) {
|
||||
// 国际化
|
||||
r.SetCtx(gi18n.WithLanguage(r.Context(), simple.GetHeaderLocale(r.Context())))
|
||||
|
||||
// 链路追踪
|
||||
if g.Cfg().MustGet(r.Context(), "jaeger.switch").Bool() {
|
||||
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)
|
||||
}
|
||||
|
||||
data := make(g.Map)
|
||||
if _, ok := s.NotRecordRequest[r.URL.Path]; ok {
|
||||
data["request.body"] = gjson.New(nil)
|
||||
} else {
|
||||
data["request.body"] = gjson.New(r.GetBodyString())
|
||||
}
|
||||
|
||||
contexts.Init(r, &model.Context{
|
||||
Data: data,
|
||||
Module: getModule(r.URL.Path),
|
||||
})
|
||||
|
||||
if len(r.Cookie.GetSessionId()) == 0 {
|
||||
r.Cookie.SetSessionId(gctx.CtxId(r.Context()))
|
||||
}
|
||||
|
||||
r.SetCtx(r.GetNeverDoneCtx())
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
func getModule(path string) (module string) {
|
||||
slice := strings.Split(path, "/")
|
||||
if len(slice) < 2 {
|
||||
module = consts.AppDefault
|
||||
return
|
||||
}
|
||||
|
||||
if slice[1] == "" {
|
||||
module = consts.AppDefault
|
||||
return
|
||||
}
|
||||
return slice[1]
|
||||
}
|
||||
|
||||
// CORS allows Cross-origin resource sharing.
|
||||
func (s *sMiddleware) CORS(r *ghttp.Request) {
|
||||
r.Response.CORSDefault()
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
// DemoLimit 演示系统操作限制
|
||||
func (s *sMiddleware) DemoLimit(r *ghttp.Request) {
|
||||
if !simple.IsDemo(r.Context()) {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method == http.MethodPost {
|
||||
if _, ok := s.DemoWhiteList[r.URL.Path]; ok {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
response.JsonExit(r, gcode.CodeNotSupported.Code(), "演示系统禁止操作!")
|
||||
return
|
||||
}
|
||||
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
// Addon 插件中间件
|
||||
func (s *sMiddleware) Addon(r *ghttp.Request) {
|
||||
var ctx = r.Context()
|
||||
|
||||
if contexts.Get(ctx).Module == "" {
|
||||
g.Log().Warning(ctx, "application module is not initialized.")
|
||||
return
|
||||
}
|
||||
|
||||
// 替换掉应用模块前缀
|
||||
path := gstr.Replace(r.URL.Path, "/"+contexts.Get(ctx).Module+"/", "", 1)
|
||||
ss := gstr.Explode("/", path)
|
||||
if len(ss) == 0 {
|
||||
g.Log().Warning(ctx, "addon was not recognized.")
|
||||
return
|
||||
}
|
||||
|
||||
module := addons.GetModule(ss[0])
|
||||
if module == nil {
|
||||
g.Log().Warningf(ctx, "addon module = nil, name:%v", ss[0])
|
||||
return
|
||||
}
|
||||
|
||||
sk := module.GetSkeleton()
|
||||
if sk == nil {
|
||||
g.Log().Warningf(ctx, "addon skeleton = nil, name:%v", ss[0])
|
||||
return
|
||||
}
|
||||
|
||||
contexts.SetAddonName(ctx, sk.Name)
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
// DeliverUserContext 将用户信息传递到上下文中
|
||||
func (s *sMiddleware) DeliverUserContext(r *ghttp.Request) (err error) {
|
||||
user, err := token.ParseLoginUser(r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch user.App {
|
||||
case consts.AppAdmin:
|
||||
if err = service.AdminSite().BindUserContext(r.Context(), user); err != nil {
|
||||
return
|
||||
}
|
||||
default:
|
||||
contexts.SetUser(r.Context(), user)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 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++ {
|
||||
if validate.InSliceExistStr(pathList[i], path) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 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++ {
|
||||
if validate.InSliceExistStr(pathList[i], path) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Package middleware
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"hotgo/internal/library/response"
|
||||
"hotgo/internal/service"
|
||||
)
|
||||
|
||||
// Blacklist IP黑名单限制中间件
|
||||
func (s *sMiddleware) Blacklist(r *ghttp.Request) {
|
||||
if err := service.SysBlacklist().VerifyRequest(r); err != nil {
|
||||
response.JsonExit(r, gerror.Code(err).Code(), err.Error())
|
||||
}
|
||||
r.Middleware.Next()
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
// Package middleware
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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/library/location"
|
||||
"hotgo/internal/library/response"
|
||||
)
|
||||
|
||||
// Develop 开发工具白名单过滤
|
||||
func (s *sMiddleware) Develop(r *ghttp.Request) {
|
||||
ips := g.Cfg().MustGet(r.Context(), "hggen.allowedIPs").Strings()
|
||||
if len(ips) == 0 {
|
||||
response.JsonExit(r, gcode.CodeNotSupported.Code(), "请配置生成白名单!")
|
||||
return
|
||||
}
|
||||
|
||||
if !gstr.InArray(ips, "*") {
|
||||
clientIp := location.GetClientIp(r)
|
||||
ok := false
|
||||
for _, ip := range ips {
|
||||
if ip == clientIp {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !ok {
|
||||
response.JsonExit(r, gcode.CodeNotSupported.Code(), fmt.Sprintf("当前IP[%s]没有配置生成白名单!", clientIp))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
r.Middleware.Next()
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// Package middleware
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"hotgo/internal/global"
|
||||
"hotgo/internal/library/response"
|
||||
"hotgo/utility/validate"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// PreFilter 请求输入预处理
|
||||
// api使用gf规范路由并且XxxReq结构体实现了validate.Filter接口即可
|
||||
func (s *sMiddleware) PreFilter(r *ghttp.Request) {
|
||||
router := global.GetRequestRoute(r)
|
||||
if router == nil {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
|
||||
funcInfo := router.Handler.Info
|
||||
|
||||
// 非规范路由不处理
|
||||
if funcInfo.Type.NumIn() != 2 {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
|
||||
inputType := funcInfo.Type.In(1)
|
||||
var inputObject reflect.Value
|
||||
if inputType.Kind() == reflect.Ptr {
|
||||
inputObject = reflect.New(inputType.Elem())
|
||||
} else {
|
||||
inputObject = reflect.New(inputType.Elem()).Elem()
|
||||
}
|
||||
|
||||
// 先验证基本校验规则
|
||||
if err := r.Parse(inputObject.Interface()); err != nil {
|
||||
resp := gerror.Code(err)
|
||||
response.JsonExit(r, resp.Code(), gerror.Current(err).Error(), resp.Detail())
|
||||
return
|
||||
}
|
||||
|
||||
// 没有实现预处理
|
||||
if _, ok := inputObject.Interface().(validate.Filter); !ok {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// 执行预处理
|
||||
if err := validate.PreFilter(r.Context(), inputObject.Interface()); err != nil {
|
||||
resp := gerror.Code(err)
|
||||
response.JsonExit(r, resp.Code(), gerror.Current(err).Error(), resp.Detail())
|
||||
return
|
||||
}
|
||||
|
||||
// 过滤后的参数
|
||||
r.SetParamMap(gconv.Map(inputObject.Interface()))
|
||||
r.Middleware.Next()
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
// Package middleware
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/library/response"
|
||||
"hotgo/utility/charset"
|
||||
"hotgo/utility/simple"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/util/gmeta"
|
||||
)
|
||||
|
||||
// ResponseHandler HTTP响应预处理
|
||||
func (s *sMiddleware) ResponseHandler(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
|
||||
// 错误状态码接管
|
||||
switch r.Response.Status {
|
||||
case 403:
|
||||
r.Response.Writeln("403 - 网站拒绝显示此网页")
|
||||
return
|
||||
case 404:
|
||||
r.Response.Writeln("404 - 你似乎来到了没有知识存在的荒原…")
|
||||
return
|
||||
}
|
||||
|
||||
contentType := getContentType(r)
|
||||
// 已存在响应
|
||||
if contentType != consts.HTTPContentTypeStream && r.Response.BufferLength() > 0 { // && contexts.Get(r.Context()).Response != nil
|
||||
return
|
||||
}
|
||||
|
||||
switch contentType {
|
||||
case consts.HTTPContentTypeHtml:
|
||||
s.responseHtml(r)
|
||||
return
|
||||
case consts.HTTPContentTypeXml:
|
||||
s.responseXml(r)
|
||||
return
|
||||
case consts.HTTPContentTypeStream:
|
||||
case consts.HTTPContentTypeOctetStream:
|
||||
default:
|
||||
responseJson(r)
|
||||
}
|
||||
}
|
||||
|
||||
// responseHtml html模板响应
|
||||
func (s *sMiddleware) responseHtml(r *ghttp.Request) {
|
||||
code, message, resp := parseResponse(r)
|
||||
if code == gcode.CodeOK.Code() {
|
||||
return
|
||||
}
|
||||
|
||||
r.Response.ClearBuffer()
|
||||
_ = r.Response.WriteTplContent(simple.DefaultErrorTplContent(r.Context()), g.Map{"code": code, "message": message, "stack": resp})
|
||||
}
|
||||
|
||||
// responseXml xml响应
|
||||
func (s *sMiddleware) responseXml(r *ghttp.Request) {
|
||||
code, message, data := parseResponse(r)
|
||||
response.RXml(r, code, message, data)
|
||||
}
|
||||
|
||||
// responseJson json响应
|
||||
func responseJson(r *ghttp.Request) {
|
||||
code, message, data := parseResponse(r)
|
||||
response.RJson(r, code, message, data)
|
||||
}
|
||||
|
||||
// parseResponse 解析响应数据
|
||||
func parseResponse(r *ghttp.Request) (code int, message string, resp interface{}) {
|
||||
ctx := r.Context()
|
||||
err := r.GetError()
|
||||
if err == nil {
|
||||
return gcode.CodeOK.Code(), "操作成功", r.GetHandlerResponse()
|
||||
}
|
||||
|
||||
// 是否输出错误堆栈到页面
|
||||
if simple.Debug(ctx) {
|
||||
message = gerror.Current(err).Error()
|
||||
if getContentType(r) == consts.HTTPContentTypeHtml {
|
||||
resp = charset.SerializeStack(err)
|
||||
} else {
|
||||
resp = charset.ParseErrStack(err)
|
||||
}
|
||||
} else {
|
||||
message = consts.ErrorMessage(gerror.Current(err))
|
||||
}
|
||||
|
||||
code = gerror.Code(err).Code()
|
||||
|
||||
// 记录异常日志
|
||||
// 如果你想对错误做不同的处理,可以通过定义不同的错误码来区分
|
||||
// 默认-1为安全可控错误码只记录文件日志,非-1为不可控错误,记录文件日志+服务日志并打印堆栈
|
||||
if code == gcode.CodeNil.Code() {
|
||||
g.Log().Stdout(false).Infof(ctx, "exception:%v", err)
|
||||
} else {
|
||||
g.Log().Errorf(ctx, "exception:%v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func getContentType(r *ghttp.Request) (contentType string) {
|
||||
contentType = r.Response.Header().Get("Content-Type")
|
||||
if contentType != "" {
|
||||
return
|
||||
}
|
||||
|
||||
mime := gmeta.Get(r.GetHandlerResponse(), "mime").String()
|
||||
if mime == "" {
|
||||
contentType = consts.HTTPContentTypeJson
|
||||
} else {
|
||||
contentType = mime
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// Package middleware
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"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()
|
||||
path = gstr.Replace(r.URL.Path, simple.RouterPrefix(ctx, consts.AppWebSocket), "", 1)
|
||||
)
|
||||
|
||||
// 不需要验证登录的路由地址
|
||||
if s.IsExceptLogin(ctx, consts.AppWebSocket, path) {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// 将用户信息传递到上下文中
|
||||
if err := s.DeliverUserContext(r); err != nil {
|
||||
response.JsonExit(r, gcode.CodeNotAuthorized.Code(), err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
r.Middleware.Next()
|
||||
}
|
||||
Reference in New Issue
Block a user