nginx - Nginx - 如何创建将与 auth_request 模块一起使用的自定义请求
问题描述
首先:我不是 nginx 专家。非常新手。
我正在尝试使用 nginx 进行身份验证来保护第 3 方软件(真的 - 只是验证请求具有有效的 OAuth2 Bearer 令牌)
HTTP 请求将在身份验证标头中包含一个 OAuth2 不记名令牌。
例如授权:承载 eyJhbGciOiJSUzI1NiIsImtpZ....H5w
我有一个 OAuth2 服务器 (UAA),它有一个 api,如果令牌有效,我可以在其中调用http://myuaa/check_token?token=eyJhbGciOiJSUzI1NiIsImtpZ....H5w来取回 2XX 或 4XX。一个复杂的问题是该服务器确实需要基本身份验证才能调用 /check_token 端点。
我尝试使用映射来解析授权标头中的令牌,但没有运气。
只是有点不知所措。
也许这不适合 Nginx?
nginx.conf 的相关部分
# this map isnt working as I thought it might
http {
...
map $http_authorization $token {
~Bearer(?<token>abc) $token;
}
...
# test just to see if the authorization header is being parsed and passed - no luck
location /oauth {
proxy_set_header X-my-header $token;
proxy_set_header X-another-header value;
proxy_set_header Authorization "Basic basdasdfasdf";
proxy_pass http://localhost:8080;
}
对 nginx 正在保护的第 3 方服务器的预期请求:
<GET|POST|PUT|DELETE> /anyurl HTTP1/1.1
..
Authorization: Bearer eyJhbGciOiJSUzI1NiIsImtpZ....H5w
..
转发到 UAA 服务器以验证令牌的预期请求
GET /check_token?token=eyJhbGciOiJSUzI1NiIsImtpZ....H5w
..
Authorization Basic asfasdfdf
..
解决方案
您的map
指令不起作用,命名组token
以某种方式干扰$token
变量,这些定义中的任何一个都可以工作:
map $http_authorization $token {
~^Bearer\s+([\S]+)$ $1;
}
或者
map $http_authorization $token {
~^Bearer\s+(?<bearer>[\S]+)$ $bearer;
}
完整的工作配置将如下所示:
map $http_authorization $token {
~^Bearer\s+(?<bearer>[\S]+)$ $bearer;
}
server {
...
location / {
auth_request /uaa;
...
}
location /uaa {
internal;
proxy_pass_request_body off;
proxy_set_header Authorization "Basic your_base64_auth_string";
proxy_set_header Content-Length "";
proxy_pass http://localhost:8080/check_token?token=$token;
}
}
推荐阅读
- python - 我正在尝试在 python 中使用 selenium 提取 i-frame 内的评论,但无法访问内部 HTML。?
- powershell - 安排 powershell 脚本每 20 分钟运行一次
- c++ - 私有继承和组合的区别
- eclipse - Eclipse 2020-03 未打开
- cloud-foundry - 在部署期间传递应用程序配置(例如 CF PUSH)
- r - 尽管设置了 bbox,但 R tmap 给出的范围略有不同
- java - 使用 @Autowired 时出现 NullPointerException
- airflow - 我们能否以某种方式使用一些技巧在气流中实现任务的循环运动?大多数情况下这是不可能的,但有什么办法吗?
- oop - CoR 和 Decorator 有什么区别?为什么 CoR 是一种行为模式?为什么装饰器是结构模式?
- javascript - 编译器不接受我的 JSON 表示法