Golang中间件链:Logger中间件如何访问下游上下文信息?
我正在尝试构建Golang HTTP中间件链以实现以下内容:
- 请求Logger中间件(有或没有身份验证信息,例如tenant -iD) - > 来登录每个经过身份验证和未经验证的请求
- 身份验证中间件
- 授权中间软件
- 使用用户信息
所有中间件零件使用context.withvalue函数
处理程序。我目前面临的问题是请求Logger中间件无法访问用户信息(记录租户ID)。
我尝试使用request.withContext
和request.context
方法,但是request.withContext
不会更改Logger Middleware中的请求。
Logger中间件如何访问下游上下文信息?
I'm trying to build a Golang http middleware chain to achieve the following:
- Request logger middleware (with or without authentication information e.g. tenant-id) -> log for every authenticated and unauthenticated request
- Authentication middleware
- Authorization middleware
- Handlers using the user info
All middleware parts pass their information using the context.WithValue function.
The problem I'm currently facing is that the request logger middleware cannot access the user information (to log a tenant id).
I tried using the request.WithContext
and request.Context
methods, but request.WithContext
doesn't change the request in the logger middleware.
How can the logger middleware access downstream context information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论