如何设置 HAProxy 以将访问令牌添加到客户端请求

How to setup HAProxy to add access token to client requests

我有一个客户端只能在没有身份验证信息的情况下发出请求。

我想使用 HAProxy 或类似的代理解决方案为这些客户端请求添加 OAuth 身份验证。

我已经成功地向客户端请求添加了 Bearer 令牌。请参阅下面的 haproxy.cfg 和一些占位符。

frontend front
  mode http
  bind *:8080
  default_backend   servers
  http-request add-header Authorization "Bearer {{ .Env.ACCESS_TOKEN}}"

backend servers
  mode http
  server server1 myserver.com:443 ssl

问题是访问令牌的 TTL 为 24 小时。所以我需要定期刷新它们或获取新令牌。

HAProxy 已经支持这个了吗?

我可以编写一些脚本来定期获取新的访问令牌、更新配置并重新启动 HAProxy。当 运行 HAProxy 在 docker 中时,这是一个好方法吗?有没有更好的解决方案?

您可以尝试 create/test 您的脚本使用 Lua, it is now supported in the latest versions, check How Lua runs in HAProxy

这方面的一个例子,但使用 Nginx + Lua, can be found in this project: https://github.com/jirutka/ngx-oauth