为 HTTPS 连接生成的 URL 在 Azure Web App 中以普通 HTTP 的形式出现

Generated URLs for HTTPS connections are coming out as plain HTTP in Azure Web App

我在 Tomcat 上部署了一个 Java webapp 运行 到 Azure App Service。身份验证通过 Azure AD 处理。在本地环境中似乎一切正常。

当我们将应用程序部署到 Azure 时,httpRequest.getScheme() 总是 return HTTP,无论调用是否来自 HTTPS 端点。

因此,重定向 URL 是使用 HTTP 端点构建的,与 Azure AD 应用注册中指定的重定向 URL 不匹配。 redirectUrl 构造如下。

String currentUri = httpRequest.getRequestURL().toString(); String redirectUrl = authority + tenant + "/oauth2/authorize? response_type=code&scope=user.read.all&response_mode=form_post&redirect_uri=" + URLEncoder.encode(currentUri, "UTF-8") + "&client_id=" + clientId + "&resource=https%3a%2f%2fgraph.microsoft.com" + "&state=" + state + "&nonce=" + nonce;

我已经搜索并找到了这个 - https://creechy.wordpress.com/2011/08/22/ssl-termination-load-balancers-java/ 。负载平衡器可能会导致此类问题,我们需要修改 Tomcat 配置。

如果我们在本地服务器上部署 WAR 文件,应用程序可以正常运行。问题仅出现在 Azure 中。

redirectUrl 始终包含 http://xxxxx.azurewebsites.net but in the App registrations the redirectUrl is specified as https://xxxx.azurewebsites.net

还有其他人遇到过这个问题吗?如何避免这种情况?

我对此做了一些研究。在 Azure 网络应用程序中,它将始终使用 http 作为协议。你可以从请求头中得到真正的协议。

String currentUri = httpRequest.getRequestURL().toString();
String realProto=httpRequest.getHeader("x-forwarded-proto");
if(realProto!=null)     currentUri=currentUri.replaceFirst("http",realProto);