Oauth2-proxy - 重定向到上游 url(Django 应用程序网页)时出现 404 错误

问题描述 投票:0回答:2

我正在尝试使用 oauth2-proxy 保护 Django 应用程序

在oauth2-proxy配置中:(版本7.2.1或7.3.0)

当上游 url 设置为类似以下内容时: --upstream="http://127.0.0.1:8000" 重定向工作正常。 (它返回我在应用程序中定义的主页)

但是,如果我使用这样的上游: --upstream="http://127.0.0.1:8000/hello" 它返回 404 错误,而不是应用程序中定义的 hello 页面

页面 http://127.0.0.1:8000/hello 直接调用时工作正常,并返回 "GET /hello HTTP/1.1" 200 136

所以我想说这不是页面的问题。

这是我正在使用的命令行:

oauth2-proxy.exe ^
    --http-address=127.0.0.1:4180 ^
    --email-domain=* ^
    --cookie-secure=false ^
    --cookie-secret=adqeqpioqr809718 ^
    --upstream="http://127.0.0.1:8000/hello" ^
    --redirect-url=http://127.0.0.1:4180/oauth2/callback ^
    --oidc-issuer-url=http://127.0.0.1:28081/auth/realms/testrealm ^
    --insecure-oidc-allow-unverified-email=true ^
    --provider=keycloak-oidc ^
    --client-id=oauth2_proxy ^
    --ssl-insecure-skip-verify=true ^
    --client-secret=L2znXLhGX4N0j3nsZYxDKfdYpXHMGDkX ^
    --skip-provider-button=true 

当 oauth2-proxy 成功重定向(--upstream="http://127.0.0.1:8000")时,我会得到该页面和以下输出:

这是 oauth2-proxy 的输出:

[2022/09/08 10:52:06] [proxy.go:89] mapping path "/" => upstream "http://127.0.0.1:8000"
[2022/09/08 10:52:06] [oauthproxy.go:148] OAuthProxy configured for Keycloak OIDC Client ID: oauth2_proxy
[2022/09/08 10:52:06] [oauthproxy.go:154] Cookie settings: name:_oauth2_proxy secure(https):false httponly:true expiry:168h0m0s domains: path:/ samesite: refresh:disabled
[2022/09/08 10:57:01] [oauthproxy.go:866] No valid authentication in request. Initiating login.
127.0.0.1:54337 - 9bbfcf75-da91-487a-a55e-40472e4adb23 - - [2022/09/08 10:57:01] 127.0.0.1:4180 GET - "/" HTTP/1.1 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.27" 302 380 0.001
127.0.0.1:54337 - e0d8ed12-e4dd-4da6-9fbb-cf689fc53f8f - [email protected] [2022/09/08 10:57:09] [AuthSuccess] Authenticated via OAuth2: Session{email:[email protected] user:93547bcc-2441-414a-9149-c7533c4f5d23 PreferredUsername:testuser token:true id_token:true created:2022-09-08 10:57:09.789934 -0300 -03 m=+303.019857301 expires:2022-09-08 11:02:09.7839238 -0300 -03 m=+603.013847101 refresh_token:true groups:[role:offline_access role:uma_authorization role:default-roles-testrealm role:account:manage-account role:account:manage-account-links role:account:view-profile]}
[2022/09/08 10:57:09] [session_store.go:163] WARNING: Multiple cookies are required for this session as it exceeds the 4kb cookie limit. Please use server side session storage (eg. Redis) instead.
127.0.0.1:54337 - e0d8ed12-e4dd-4da6-9fbb-cf689fc53f8f - - [2022/09/08 10:57:09] 127.0.0.1:4180 GET - "/oauth2/callback?state=ahuKzCYr7jR4P4mmjniIt67TttZKyxGv4mLfEwKlQio%3A%2F&session_state=86ac9bd1-9756-4916-83e9-ec0496b5b767&code=df3940e5-58f5-49ac-a821-5607f0f2faae.86ac9bd1-9756-4916-83e9-ec0496b5b767.cd30a162-8e4d-4a2d-bff6-168e444aed92" HTTP/1.1 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.27" 302 24 0.029
127.0.0.1:54337 - d58ace6e-afe9-4737-9b12-dbc17fdd0ca2 - [email protected] [2022/09/08 10:57:09] 127.0.0.1:4180 GET / "/" HTTP/1.1 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.27" 200 138 0.005

在 Django 方面我得到:

**"GET / HTTP/1.1" 200 138**


当 oauth2-proxy 无法重定向 --upstream="http://127.0.0.1:8000/hello") 时,我得到以下输出:

这是 oauth2-proxy 的输出:

[2022/09/08 10:33:58] [proxy.go:89] mapping path "/hello" => upstream "http://127.0.0.1:8000/hello"
[2022/09/08 10:33:58] [oauthproxy.go:148] OAuthProxy configured for Keycloak OIDC Client ID: oauth2_proxy
[2022/09/08 10:33:58] [oauthproxy.go:154] Cookie settings: name:_oauth2_proxy secure(https):false httponly:true expiry:168h0m0s domains: path:/ samesite: refresh:disabled
[2022/09/08 10:37:20] [oauthproxy.go:866] No valid authentication in request. Initiating login.
127.0.0.1:53615 - 54c0f3d8-b3c0-4d48-8353-fe69be0e4500 - - [2022/09/08 10:37:20] 127.0.0.1:4180 GET - "/" HTTP/1.1 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.27" 302 380 0.001
127.0.0.1:53615 - 0bec934e-05a3-4cc8-9306-fffc28597c8f - [email protected] [2022/09/08 10:37:28] [AuthSuccess] Authenticated via OAuth2: Session{email:[email protected] user:93547bcc-2441-414a-9149-c7533c4f5d23 PreferredUsername:testuser token:true id_token:true created:2022-09-08 10:37:28.6527488 -0300 -03 m=+210.486252601 expires:2022-09-08 10:42:28.6468518 -0300 -03 m=+510.480355601 refresh_token:true groups:[role:offline_access role:uma_authorization role:default-roles-testrealm role:account:manage-account role:account:manage-account-links role:account:view-profile]}
[2022/09/08 10:37:28] [session_store.go:163] WARNING: Multiple cookies are required for this session as it exceeds the 4kb cookie limit. Please use server side session storage (eg. Redis) instead.
127.0.0.1:53615 - 0bec934e-05a3-4cc8-9306-fffc28597c8f - - [2022/09/08 10:37:28] 127.0.0.1:4180 GET - "/oauth2/callback?state=nox0LM3fIlVU1kamoLBaktByeLCcIWiBvRLdHFIuhd4%3A%2F&session_state=808c0654-c9e7-4593-b5dc-95d3231438ea&code=e220414d-e949-4e2d-8d33-55de96f8f5d4.808c0654-c9e7-4593-b5dc-95d3231438ea.cd30a162-8e4d-4a2d-bff6-168e444aed92" HTTP/1.1 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.27" 302 24 0.024
127.0.0.1:53615 - 9454773f-cade-46fe-870f-70d09fc49ffb - [email protected] [2022/09/08 10:37:28] 127.0.0.1:4180 GET - "/" HTTP/1.1 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.27" 404 19 0.000

在 Django 方面我得到:

什么也没有。由于 Django 应用程序从未访问过,因此没有日志。

您能帮我找出可能发生的情况吗?我会非常感激!!

这似乎不是应用程序的问题,因为直接调用时页面工作正常。 如果我的 oauth2-proxy 命令行/配置中有错误,我希望有人指出该错误,以便我可以更正它。

否则,任何提示也将不胜感激。

我在oauth2-proxy的日志中唯一注意到的是,无论我在--upstream中输入什么,最终的GET(我认为它是重定向到上游)如下: GET - "/ " ...两次尝试都是相同的,并且只在第一次成功,因为它匹配 [proxy.go:89] 映射路径“/”

django keycloak custom-error-pages oauth2-proxy upstream
2个回答
0
投票

它给出 404 错误的原因是配置 --upstreams 指向一个 url,代理在经过身份验证后将把请求传递到该 url,但除非您特别要求,否则它不会重定向到该地址在最初的请求中。

因此发出请求的正确方法是http://127.0.0.1:4180/hello,其中包括您想要到达的端点的整个路径。 (而不是例如 http://127.0.0.1:4180


0
投票

如果您使用环境变量,请注意上游环境变量末尾有一个“S”。 即:

--upstream http://myfqdn:myport
应该是
OAUTH2_PROXY_UPSTREAMS=http://myfqdn:myport

© www.soinside.com 2019 - 2024. All rights reserved.