authentication - 带有 cookie 的 nginx auth_request
问题描述
可能是这个问题的副本,但它没有答案,我在那里尝试了这个建议,但无法让它发挥作用。我需要在代理之前授权每个请求,我试图通过 cookie 来做到这一点,但 cookie 值未在任何后续请求上设置。互联网上的大多数地方都推荐以下内容
server {
auth_request /auth;
location /auth {
internal;
proxy_pass http://auth:8080/auth;
auth_request_set $saved_set_cookie $upstream_http_set_cookie;
add_header Set-Cookie $saved_set_cookie;
}
}
但这似乎不起作用。如上面的问题所述,我什至尝试使用自定义标题来查看是否可以看到它,但它不起作用。
server {
auth_request /auth;
location /auth {
internal;
proxy_pass http://auth:8080/auth;
auth_request_set $saved_set_cookie $upstream_http_set_cookie;
add_header X-COOKIE-TEST $saved_set_cookie;
}
}
如果我直接访问身份验证服务器,我会看到 cookie 已设置
解决方案
在https://github.com/nginxinc/NGINX-Demos/blob/331fd357e6e1813b5d41aed48880cf274d31dcee/oauth2-token-introspection-oss/frontend.conf#L29找到了一个可行的解决方案,它非常简单(Nginx 1.18.0):
location / {
auth_request /authz;
auth_request_set $new_cookie $sent_http_set_cookie; # use sent_http_*, not upstream_http_*
add_header Set-Cookie $new_cookie;
add_header X-Test $sent_http_set_cookie; # it's even working directly
}
推荐阅读
- python - 将数据帧 2D 转换为 1D
- python - 在python中构建路径
- python - Python 忽略 if/elif 语句
- azure - 比较两个表以在 Azure 数据工厂中动态查找缺失的行
- next.js - 如何使用 Next.js router.push 到 Formik 的动态 api 路由,得到 404(未找到)错误
- microsoft-graph-api - Microsoft Graph API 可以获取 Teams 用户的状态消息吗?
- java - 将项目添加到列表但打印时为空 [JAVA]
- python - 需要重新格式化文本文件,将扬声器文本向上移动到扬声器标签
- python - 如何打破计数
- c# - WPF 中是否有与 imagebox 等效的 picturebox.graphics() winforms?