【问题标题】:Flask-Dance uses localhost instead of domain when redirectingFlask-Dance 重定向时使用 localhost 而不是域
【发布时间】:2020-01-16 16:28:11
【问题描述】:

我有一个使用 Flask-Dance 进行 Google 登录身份验证的应用。当我尝试登录时,它说我正在从 127.0.0.1:9852 重定向,这是我的应用程序在服务器中运行的位置,但是我有一个 apache 配置,它将服务器名称分配给该地址 (xxx.xxx.com )

我在我的 Google 控制台中注册了授权 URI 中的域。不过,当我尝试访问登录部分时,它会说“请求中的重定向 URI,http://127.0.0.1:9852/google/authorized,与授权给 OAuth 客户端的不匹配。”

所以我确实注册了该地址,它确实让我登录,但是当它尝试重定向时,它说它找不到服务器“127.0.0.1”。无论如何我可以使用我的域作为实际授权的 URI 吗?

这是我的蓝图:

blueprint = make_google_blueprint(
    client_id="id",
    client_secret="secret",
    scope=['https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email',
           'openid'],
    storage=SQLAlchemyStorage(OAuth, db.session, user=current_user),
    redirect_url='questions.view_all')

编辑:这是 Apache2 conf 文件:

    <VirtualHost *:80>
            ServerName xxx.xxx.com
            ServerAlias xxx.xxx.com
            ServerAdmin em@il.com
            # Redirect http to https
            RedirectMatch 301 (.*) https://xxx.xxx.com$1
    </VirtualHost>

    <VirtualHost _default_:443>
            ServerName xxx.xxx.com
            ServerAlias xxx.xxx.com
            ServerAdmin em@il.com

            #   Enable/Disable SSL for this virtual host.
            SSLEngine On
            SSLProxyEngine On

            # Web root
            ProxyPass /  http://127.0.0.1:9852/
            ProxyPassReverse /  http://127.0.0.1:9852/

            # Log configuration
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined

            # Self signed SSL Certificate file
            SSLCertificateFile      /etc/apache2/ssl/certs/cert.crt
            SSLCertificateKeyFile /etc/apache2/ssl/private/cert.key
    </VirtualHost>

【问题讨论】:

    标签: flask google-oauth flask-dance


    【解决方案1】:

    我在我的 Google 控制台中注册了授权 URI 中的域

    full redirect URL should be registered 用于 Google 控制台中的 OAUTH 客户端应用程序。

    当我尝试登录时,它说我正在从 127.0.0.1:9852 重定向

    您给定的服务器配置为您的应用程序提供了 gunicorn 和 Apache 配置为代理服务器。

    在某些方面,请求没有传递到 gunicorn 服务器,并没有为werkzeug to determine the right hostname 提供足够的信息。

    我建议在您的VirtualHost 中使用ProxyPreserveHost directive 以使传入的主机能够传递给gunicorn。

    【讨论】:

    • 感谢您的回答!该应用程序存在于 Docker 容器中,使用 gunicorn 提供服务。我已经编辑了 apache conf 文件以供参考。另外,我正在注册完整的 URL,包括 https。
    • 再次感谢您的回答!该指令确实有效,现在我的请求正在通过我的域收到。我现在遇到的唯一问题是它重定向为 http 而不是 https。我可以打开不安全的传输,但我绝对不想这样做。
    • 您可能需要 Apache 设置 X-Forwarded-Proto 标头以使 Flask 生成 HTTPS URL。见flask.palletsprojects.com/en/1.1.x/deploying/wsgi-standalone/…
    猜你喜欢
    • 1970-01-01
    • 2020-03-11
    • 2014-09-19
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 2018-12-26
    • 2020-03-08
    相关资源
    最近更新 更多