【问题标题】:google oauth 2.0 redirect_uri_mismatch (aws ec2 + route53 + nginx + uwgsi + flask + socketio)google oauth 2.0 redirect_uri_mismatch (aws ec2 + route53 + nginx + uwgsi + flask + socketio)
【发布时间】:2017-09-11 00:23:21
【问题描述】:

每当我尝试在我的网站上使用 Google OAuth 2.0 时,我都会收到 redirect_uri_mismatch。我正在使用 aws ec2 + route53 + nginx + uwgsi + flask + socketio

授权的重定向 URI:必须添加括号才能删除链接。

  • (http)://example.net/auth/oauth2callback
  • (http)s://example.net/auth/oauth2callback
  • (http)://www.example.net/auth/oauth2callback
  • (http)s://www.example.net/auth/oauth2callback
  • (http)://localhost/auth/oauth2callback
  • (http)s://localhost/auth/oauth2callback

我还添加了一个尾随“/”,但它似乎没有解决任何问题。我怀疑它是 nginx 服务器设置问题或 nginx -> uwsgi -> flask 之间的一些连接问题,因为重定向 uri 谷歌的错误页面告诉我的内容与我放入谷歌 OAuth 设置的 URI 完全匹配。

这是我的 nginx 服务器配置:

server {
    listen 80;
    server_name www.example.net;

    return 301 http://example.net;
}

server {
    listen 80;
    server_name example.net;

    return 301 https://example.net;
}

server {
    listen 443;
    server_name example.net;

    ssl on;
    ssl_certificate /certs.pem;
    ssl_certificate_key /private.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers BLANK;
    ssl_session_cache shared:SSL:50m;
    ssl_dhparam /dhparam.pem;
    ssl_prefer_server_ciphers on;

    add_header Strict-Transport-Security "max-age=31536000";

    client_max_body_size 5M;

    location / {
        include uwsgi_params;

        uwsgi_pass unix:/filename.sock;
    }
}

我已经查看了很多关于此的文件。大多数人使用代理并且必须在服务器配置中添加一些行来处理这个问题,但是由于我没有使用代理,我相信我不必在我的配置中添加任何这些。 (为了以防万一,我尝试了一些解决方案,但他们似乎仍然没有解决它。)

在不使用 nginx 和 uwsgi 的情况下,我能够让 OAuth 功能在 localhost 上运行。

提前致谢!

编辑--
@TarunLalwani 好的,所以我让它使用 proxy_pass 使它工作一半。通过只添加 proxy_pass 而没有其他任何内容,我可以进入 Google OAuth 登录页面。但是一旦我授权登录,它会将用户重定向回 127.0.0.1:5000(这是我为 proxy_pass 设置的)。我想我必须做一些反向代理设置。我在 proxy_pass 之后的位置块中添加的是这样的:

proxy_redirect off;
proxy_buffering off;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";

现在反向代理似乎可以工作,因为 OAuth 再次被破坏。但是 OAuth 2.0 将用户重定向到 http:example.net/auth/oauth2callback 而不是 http:127.0.0.1:5000/auth/oauth2callback

我通过逐行添加每个命令行并重新启动服务器来进行测试。 在添加proxy_set_header Host $host; 之前,我会得到http://127.0.0.1:5000/auth/oauth2callback 作为重定向uri,当我添加该行时,我会得到http://example.net/auth/oauth2callback。也许OAuth 被破坏的原因是因为用户被重定向到http://example.net/auth/oauth2callback https://example.net/auth/oauth2callback 的?

编辑 2 --
通过curl -v https://example.net/auth/oauth2callback 我得到以下信息:

*   Trying my.ip...
* Connected to example.net (my.ip) port 443 (#0)
* found 173 certificates in certs
* found 696 certificates in certs
* ALPN, offering http/1.1
* SSL connection using TLS1.2 / ECDHE_RSA_AES_256_GCM_SHA384
*        server certificate verification OK
*        server certificate status verification SKIPPED
*        common name: example.net (matched)
*        server certificate expiration date OK
*        server certificate activation date OK
*        certificate public key: RSA
*        certificate version: #3
*        subject: CN=example.net
*        start date: Sun, 10 Sep 2017 22:18:00 GMT
*        expire date: Sat, 09 Dec 2017 22:18:00 GMT
*        issuer: C=US,O=Let's Encrypt,CN=Let's Encrypt Authority X3
*        compression: NULL
* ALPN, server accepted to use http/1.1
> GET /auth/oauth2callback HTTP/1.1
> Host: feelsbadman.net
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 302 FOUND
< Server: nginx/1.10.3 (Ubuntu)
< Date: Tue, 12 Sep 2017 07:32:36 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 819
< Connection: keep-alive
< Location: https://accounts.google.com/o/oauth2/auth?client_id=myid-asdfasdfasdf.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Fexample.net%2Fauth%2Foauth2callback&scope=email&access_type=offline&response_type=code&include_granted_scopes=true
< Strict-Transport-Security: max-age=31536000
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
* Connection #0 to host example.net left intact
<p>You should be redirected automatically to target URL: <a href="https://accounts.google.com/o/oauth2/auth?client_id=myid-asdfasdfasdf.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Fexample.net%2Fauth%2Foauth2callback&scope=email&access_type=offline&response_type=code&include_granted_scopes=true">https://accounts.google.com/o/oauth2/auth?client_id=myid-asdfasdfasdf.apps.googleusercontent.com&redirect_uri=http://example.net/auth/oauth2callback&scope=email&access_type=offline&response_type=code&include_granted_scopes=true</a>.

【问题讨论】:

  • 您是否尝试在您的location / 块中设置proxy_set_header X-Forwarded-Host $host;?
  • @birryree 是的,我试着把它放在 uwsgi_pass 之前 :(
  • 您能在浏览器中查看返回的返回 URL 吗?
  • @TarunLalwani 是的,浏览器返回:请求中的重定向 URI,https://example.net/auth/oauth2callback,与 OAuth 客户端授权的不匹配。
  • 你能一次添加一件事吗?就像只是将 Nginx 和 proxy_pass 添加到烧瓶中,看看它是否有效。然后通过uwsgi来做,看看能不能用。尝试锻炼哪个是问题区域

标签: python nginx url-redirection uwsgi google-oauth


【解决方案1】:

您的网站是https://example.net,而您的返回网址是http://example.net/auth/oauth2callback 并被拒绝。

请确保发送给 google 的返回 url 是 https://example.net/auth/oauth2callback 并且在授权重定向 URI 中设置相同

【讨论】:

    猜你喜欢
    • 2014-02-09
    • 2015-04-03
    • 2020-11-08
    • 2021-11-05
    • 2016-02-21
    • 2015-10-03
    • 2021-06-28
    • 2021-11-13
    • 1970-01-01
    相关资源
    最近更新 更多