Flask-Dance 在重定向时使用 localhost 而不是域

Flask-Dance uses localhost instead of domain when redirecting

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

我在 Google 控制台中注册了授权 URI 中的域。不过,当我尝试访问登录部分时,它说 'The redirect URI in the request, http://127.0.0.1:9852/google/authorized, does not match the ones authorized for the OAuth client.'

所以我确实注册了那个地址,它确实让我登录,但是当它尝试重定向时它说找不到服务器“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 配置文件:

    <VirtualHost *:80>
            ServerName xxx.xxx.com
            ServerAlias xxx.xxx.com
            ServerAdmin em@il.com
            # Redirect http to https
            RedirectMatch 301 (.*) https://xxx.xxx.com
    </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>

I registered in my Google Console the domain in the authorized URIs

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

When I try to login, it says that I am redirecting from 127.0.0.1:9852

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

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

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