This commit is contained in:
2025-05-09 17:54:28 +08:00
parent 8003431fdb
commit 00c2f07769
110 changed files with 11003 additions and 576 deletions

View File

@@ -0,0 +1,43 @@
package middleware
import (
"net/http"
"tyc-server/app/main/api/internal/config"
)
type AuthInterceptorMiddleware struct {
Config config.Config
}
func NewAuthInterceptorMiddleware(c config.Config) *AuthInterceptorMiddleware {
return &AuthInterceptorMiddleware{
Config: c,
}
}
func (m *AuthInterceptorMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 从请求头中获取Authorization字段
// authHeader := r.Header.Get("Authorization")
// if authHeader != "" && strings.HasPrefix(authHeader, "Bearer ") {
// // 提取JWT令牌去除Bearer前缀
// tokenString := strings.TrimPrefix(authHeader, "Bearer ")
// // 使用jwtx.ParseJwtTokenV2()解析令牌
// claims, err := jwtx.ParseJwtTokenV2(tokenString, m.Config.AdminConfig.AccessSecret)
// if err != nil {
// resultErr := errors.Wrapf(xerr.NewErrMsg("用户名或密码错误"), "用户登录, 用户名或密码错误, 用户名: %s")
// result.HttpResult(r, w, nil, resultErr)
// return
// }
// // 如果解析失败可以添加处理代码如返回401错误
// }
// 如果没有Authorization头或解析失败继续传递到下一个处理器
// 根据业务需求可以决定是否继续处理请求
next(w, r)
}
}

View File

@@ -0,0 +1,34 @@
package middleware
import (
"context"
"net/http"
)
type contextKey string
const (
brandKey contextKey = "brand"
platformKey contextKey = "platform"
promoteKey contextKey = "promoteKey"
)
func ReqHeaderCtxMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
brand := r.Header.Get("X-Brand")
platform := r.Header.Get("X-Platform")
promoteValue := r.Header.Get("X-Promote-Key")
ctx := r.Context()
if brand != "" {
ctx = context.WithValue(ctx, brandKey, brand)
}
if platform != "" {
ctx = context.WithValue(ctx, platformKey, platform)
}
if promoteValue != "" {
ctx = context.WithValue(ctx, promoteKey, promoteValue)
}
r = r.WithContext(ctx)
next(w, r)
}
}