问题原文
原帖链接 · BackSlash · CC BY-SA 4.0
I have an authorization module which is called whenever a request is made to a private endpoint.
The module parses the token from the Authorization header, and:
- if it's invalid, it returns 401
- if it's valid, it returns 200
- if it's valid but is about to expire in X minutes, it generates a new token and returns that one in the
Authorizationheader.
"profile" is one of the private endpoints, and it's configured this way:
location /profile {
auth_request /jwtverify;
auth_request_set $authorization $upstream_http_authorization;
proxy_set_header authorization $authorization;
proxy_pass http://private-profile:80;
}
jwtverify is configured this way:
location = /jwtverify {
internal;
proxy_pass http://auth-module:8080/auth/verify;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
auth_request_set $http_authorization $upstream_http_authorization;
}
Now, everything works except for requirement no. 3: if the auth module sets the Authorization header, the client never receives it.
The flow should be like this:
- Client makes the request
auth-moduleintercepts the request and, if valid, the proxy passes it to the private service- When the response is sent, headers set by
auth-moduleshould be kept and sent to the client
I think I didn't understand properly how to combine auth_request_set, proxy_set_header, auth_request_set, it might also be that they aren't correct for this scenario.
Is there a way to accomplish this in NGINX?
已采纳解答
原帖链接 · BackSlash · CC BY-SA 4.0
Ok, I was able to do that with the help of the headers_more module.
The full configuration is:
location = /jwtverify {
internal;
proxy_pass http://auth-module:8080/auth/verify;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
}
location /profile {
auth_request /jwtverify;
# this gets called right after auth_request returns.
# it reads http "authorization" header from upstream (= auth_request)
# and sets it to the variable $auth_header
# https://stackoverflow.com/a/31485557/1759845
auth_request_set $auth_header $upstream_http_authorization;
# this gets called right before sending response to client.
# it adds the previously set variable (= "authorization"
# header from auth_request) to the response
more_set_headers "Authorization: $auth_header";
proxy_pass http://private-profile:80;
}
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
