【问题标题】:HAProxy set authorization header from cookieHAProxy 从 cookie 中设置授权标头
【发布时间】:2021-12-23 02:50:58
【问题描述】:

我有一个我无法控制的后端,它使用魔术链接工作。如果我使用魔术链接,我不必登录。我想在不暴露实际密钥的情况下在外部共享魔术链接,因为密钥会不时更改,我希望使用 haproxy 作为反向代理。我不希望链接对整个互联网开放,并且也希望使用基本身份验证。我面临的问题是后端覆盖了 Authorization 标头(它是魔术链接工作所必需的),我陷入了一个循环,每次都需要登录

我的解决方法:

  • 在第一次请求时,我请求基本身份验证(有效)
  • 然后我将其写入 cookie(这部分有效)
  • 在每个后续请求中,如果 cookie 存在,我会读取 cookie 并将 Authorization 标头设置为 cookie 值(这部分不起作用
  • 然后我运行 http_auth,在我看来它现在应该可以工作了,因为我已经覆盖了标题

但它不起作用。有什么建议吗?

userlist auth-list
  user myuser insecure-password mypass

frontend myfrontend
   bind *:80
   mode http
   acl is-path path -i -m beg /publiclink
   acl has_cookie req.hdr(X-MyAuth) -m found
   http-request set-header Authorization %[req.hdr(X-MyAuth)] if has_cookie
   http-request auth unless { http_auth(auth-list) }
   http-request set-var(txn.myhostheader) req.hdr(Authorization) if { http_auth(auth-list) !has_cookie }
   default_backend node

backend node
    mode http
    server dcnode1 192.168.0.1:8000 check
    http-response set-header set-cookie "X-MyAuth=%[var(txn.myhostheader)]; Path=/" if  { var(txn.myhostheader) -m found }
    http-request replace-path /publiclink1(.*) /magiclink\1
    http-request set-header Authorization "Key magiclink"

【问题讨论】:

    标签: haproxy


    【解决方案1】:

    我的回答与这一点有关:

    • 在每个后续请求中,如果 cookie 存在,我读取 cookie 并将 Authorization 标头设置为 cookie 值(这部分不起作用)

    在您的后端中,您设置了Set-Cookie: X-MyAuth=... 标头:

    backend node
        http-response set-header set-cookie "X-MyAuth=%[var(txn.myhostheader)]; Path=/" if  { var(txn.myhostheader) -m found }
    
    

    所以 下一个请求 包含一个 Cookie 标头,如下所示:Cookie: .* X-MyAuth=.*

    但在您的前端中,您使用X-MyAuth 标头(可能不存在):

    frontend myfrontend
       acl has_cookie req.hdr(X-MyAuth) -m found
       http-request set-header Authorization %[req.hdr(X-MyAuth)] if has_cookie
    

    您可以像这样使用X-MyAuth cookie值

    frontend myfrontend
       acl has_cookie cook(X-MyAuth) -m found
       http-request set-header Authorization %[cook(X-MyAuth)] if has_cookie
    

    【讨论】:

    • 谢谢,就是这样!
    猜你喜欢
    • 2021-11-21
    • 2015-12-02
    • 2017-08-18
    • 2017-08-30
    • 2021-04-07
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    相关资源
    最近更新 更多