This commit is contained in:
oneao committed 2025-11-28 22:41:37 +08:00
1 parent cf4291652a
commit 8408cf779f
20 files changed
+1400 -33

No files matched your search

@@ -0,0 +1,17 @@
package middleware
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
)
// Error 中间件
func Error() app.HandlerFunc {
return func(ctx context.Context, c *app.RequestContext) {
c.Next(ctx)
if err := c.Errors.Last(); err != nil {
}
}
}
@@ -0,0 +1,100 @@
package middleware
import (
"context"
"log"
"net/http"
"time"
"github.com/cloudwego/hertz/pkg/app"
"github.com/hertz-contrib/jwt"
)
var identityKey = "UserId"
type loginForm struct {
Account string `form:"username,required" json:"username,required"`
Password string `form:"password,required" json:"password,required"`
Type int `form:"type,required" json:"type,required"`
}
type User struct {
UserId int64
}
var hertzJWTMiddleware *jwt.HertzJWTMiddleware
func init() {
middleware, err := jwt.New(&jwt.HertzJWTMiddleware{
Realm: "test zone",
Key: []byte("secret key"),
Timeout: 30 * time.Minute,
MaxRefresh: 30 * 24 * time.Hour,
IdentityKey: identityKey,
// Token负载信息
PayloadFunc: func(data interface{}) jwt.MapClaims {
if v, ok := data.(*User); ok {
return jwt.MapClaims{
identityKey: v.UserId,
}
}
return jwt.MapClaims{}
},
// 解析Token取出用户信息
IdentityHandler: func(ctx context.Context, c *app.RequestContext) interface{} {
claims := jwt.ExtractClaims(ctx, c)
return &User{
UserId: claims[identityKey].(int64),
}
},
// 登录
Authenticator: func(ctx context.Context, c *app.RequestContext) (interface{}, error) {
c.Request.RequestURI()
return nil, jwt.ErrFailedAuthentication
},
// 权限控制
Authorizator: func(data interface{}, ctx context.Context, c *app.RequestContext) bool {
return true
},
// 未验证处理
Unauthorized: func(ctx context.Context, c *app.RequestContext, code int, message string) {
c.JSON(code, map[string]interface{}{
"code": code,
"message": message,
})
},
// 登录响应
LoginResponse: func(ctx context.Context, c *app.RequestContext, code int, token string, expire time.Time) {
c.JSON(http.StatusOK, map[string]interface{}{
"code": http.StatusOK,
"token": token,
"expire": expire.Format(time.RFC3339),
})
},
// 登出响应
LogoutResponse: func(ctx context.Context, c *app.RequestContext, code int) {
c.JSON(http.StatusOK, map[string]interface{}{
"code": http.StatusOK,
})
},
// 刷新Token
RefreshResponse: func(ctx context.Context, c *app.RequestContext, code int, token string, expire time.Time) {
c.JSON(http.StatusOK, map[string]interface{}{
"code": http.StatusOK,
"token": token,
"expire": expire.Format(time.RFC3339),
})
},
TokenLookup: "header: Authorization",
})
if err != nil {
log.Fatal("JWT Error:" + err.Error())
}
hertzJWTMiddleware = middleware
}
func JWT() *jwt.HertzJWTMiddleware {
return hertzJWTMiddleware
}
@@ -0,0 +1,35 @@
package middleware
import (
"cn/oneao/base-go/logx"
"context"
"time"
"github.com/cloudwego/hertz/pkg/app"
"github.com/hertz-contrib/requestid"
"go.uber.org/zap"
)
func Log() app.HandlerFunc {
return func(c context.Context, ctx *app.RequestContext) {
reqId := requestid.Get(ctx)
c = context.WithValue(c, "RequestID", reqId)
start := time.Now() // 记录开始时间
// 执行后续处理
ctx.Next(c)
// 计算耗时
duration := time.Since(start).Milliseconds()
logx.CtxInfo(c, "请求完成",
zap.String("IP", ctx.ClientIP()),
zap.String("Method", string(ctx.Method())),
zap.String("Path", string(ctx.Path())),
zap.Int("Status", ctx.GetResponse().StatusCode()),
zap.Int64("Duration", duration),
)
}
}