【问题标题】:Spring Security Azure AD redirect url issueSpring Security Azure AD 重定向 url 问题
【发布时间】:2021-07-11 05:04:56
【问题描述】:

当我尝试在 localhost 中运行时,它工作正常。但是,当我尝试在负载均衡器后面运行相同的程序时,会出现以下错误:

AADSTS50011: The reply URL specified in the request does not match the reply URLs configured for the application: '<clien-id>'.

我已在 AzureAD 中使用负载均衡器 URL 注册了应用程序。但是当我发送请求时,重定向 URL 仍然是 localhost,如下所示。

https://login.microsoftonline.com/common/oauth2/authorize?response_type=code&client_id=XXX&...**redirect_uri=localhost:8080/login/oauth2/code/azure**&nonce=...

我希望我的应用程序将负载均衡器 URL 插入为 redirect_url 的值(而不是 localhost)。

我尝试了以下建议的解决方案,但仍然没有成功:

Redirect URL for Spring OAuth2 app on Azure with Active Directory: Invalid Redirect URI Parameter

Spring Boot using Azure OAuth2 - reply URL does not match error

提前致谢。

【问题讨论】:

  • 这对您有帮助吗? stackoverflow.com/questions/67056387/…
  • 谢谢卡尔。我解码了,这就是我粘贴在下面的内容。我需要一些可以在我的 Java Spring 引导应用程序中自定义的方法。
  • 您需要在 Azure 门户和代码中更改 redirect_uri。如果两个网址不相同,就会报错。
  • 是的,我检查过很多次,但看起来没问题。仍在尝试..谢谢

标签: spring-security oauth-2.0 azure-active-directory spring-security-oauth2


【解决方案1】:

当您使用负载均衡器/代理时,您需要添加一些额外的配置以正确解析重定向 URL。

负载均衡器通常应用标准RFC7239 "Forwarded Headers",如X-Forwarded-Proto 和X-Forwarded-Host。在这种情况下,应该在应用以下两个配置后正确计算重定向 url。 (以 Tomcat 场景为例)

server.forward-headers-strategy=NATIVE

"如果代理添加常用的X-Forwarded-For和 X-Forwarded-Proto 标头,将 server.forward-headers-strategy 设置为 NATIVE 足以支持这些。”

server.tomcat.redirect-context-root=false

如果您使用 Tomcat 并在代理端终止 SSL, server.tomcat.redirect-context-root 应设置为 false。这 允许在任何重定向之前遵守 X-Forwarded-Proto 标头 执行。

如果您在 Spring Security 的客户端配置中使用占位符作为基本 URL 的占位符,则上述配置有效,例如 {baseUrl}/login/oauth2/code/{registrationId}。这样,{baseUrl} 占位符由 Spring Security 动态解析,具体取决于它是否位于负载均衡器后面(https://your-lb-url.com 与 http://localhost:8080)。

官方文档中的更多信息:

【讨论】:

  • 嗯,有多个问题。配置的 URL 有一个空格 - 一个错字。在 application.properties 中,必须添加以下内容:spring.security.oauth2.client.registration.azure.redirect-uri=
猜你喜欢
  • 2021-01-23
  • 2021-11-22
  • 2010-12-18
  • 2016-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-18
  • 1970-01-01
相关资源
最近更新 更多