首页 > 解决方案 > 使用 nginx 和 docker 部署时,Azure 验证的 Flask 应用程序中的重定向 URI 不正确

问题描述

我有一个基于以下说明构建的烧瓶应用程序,它允许我对基于 Azure AD 的用户进行身份验证。 https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-v2-python-webapp

该应用程序在测试时运行良好localhost:5000。现在我想使用 docker 和 nginx 反向代理将它部署到生产服务器。我创建了一个 docker 容器,以便将 docker 端口映射到 localhost 上的端口 6000。然后我proxy_pass在 nginx 配置中添加了一个将流量传递给 docker 容器。

nginx.conf

location /app/authenticated-app/ {
  proxy_pass http://localhost:6000/;
  proxy_redirect default;
}

使用此配置,我可以通过以下方式转到登录页面,https://server/app/authenticated-app但是,当我单击登录时,转到 azure 的请求的查询参数redirect_uri设置为http://localhost:6000/getToken. 因此,一旦我完成登录,应用程序就会被重定向到该 URL。有谁知道如何解决这个问题并将其重定向到正确的网址。我已经在 azure 门户https://server/app/authenticated-app/getToken下添加了。redirect_uri

标签: dockernginxflaskazure-active-directorymsal

解决方案


我有一个类似的问题,nginx 和我的烧瓶应用程序都在同一堆栈的 docker 容器中运行并使用自签名 SSL 证书。

我的 nginx 重定向请求如下:

proxy_pass http://$CONTAINER_NAME:$PORT;

并且 msal 应用程序在构建它时使用该 URLredirect_uri

def _build_auth_code_flow(authority=None, scopes=None):
    return _build_msal_app(authority=authority).initiate_auth_code_flow(
        scopes or [],
        redirect_uri=url_for("auth.authorized", _external=True))

我通过在我的文件中硬编码我想要的返回 URL(与我在 azure 应用程序注册中配置的相同)config.py并将其用于redirect_uri

def _build_auth_code_flow(authority=None, scopes=None):
    return _build_msal_app(authority=authority).initiate_auth_code_flow(
        scopes or [],
        redirect_uri=current_app.config['HARDCODED_REDIRECT_URL_MICROSOFT'])

在我的情况下,那url将是https://localhost/auth/redirect/. 我还需要配置我的 nginx 以将所有请求从 http 重定向到 https:

events {}
http {

  server {
    listen 80;
    server_name localhost;
    return 301 https://localhost$request_uri;

  }
...

推荐阅读