Golang中间件链:Logger中间件如何访问下游上下文信息?

发布于 2025-01-19 10:45:04 字数 429 浏览 4 评论 0原文

我正在尝试构建Golang HTTP中间件链以实现以下内容:

  • 请求Logger中间件(有或没有身份验证信息,例如tenant -iD) - > 来登录每个经过身份验证和未经验证的请求
  • 身份验证中间件
  • 授权中间软件
  • 使用用户信息

所有中间件零件使用context.withvalue函数

处理程序。我目前面临的问题是请求Logger中间件无法访问用户信息(记录租户ID)。

我尝试使用request.withContextrequest.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文