【问题标题】:Issue with Spring Security w/ Azure AD & the redirect_uri behind a reverse proxy带有 Azure AD 和反向代理后面的 redirect_uri 的 Spring Security 问题
【发布时间】:2021-04-28 08:57:54
【问题描述】:

我一直在互联网上寻找解决我的问题的方法,如下 -

我们有一个带有 Spring Security OAuth2 和 Azure Active Directory 集成的 Spring Boot 应用程序来管理我们的外部身份验证。当我们尝试登录时,登录URL中的redirect_uri是错误的。

redirect_uri 没有像 application.yaml 和 Azure AD 门户中定义的那样显示“&redirect_uri=https://10.10.10.10/v1/login/oauth2/code/”,而是显示为“&redirect_uri=http ://10.10.10.10:8080/v1/login/oauth2/code/”。如您所见,它正在删除 https 方案并添加端口,这会导致由于 redirect_uri 不匹配而导致登录错误。需要注意的是,这在本地可以正常工作,但是当部署到反向代理后面的环境时,它就不起作用了。

我的尝试 -

将以下属性添加到 application.yaml -

  • server.forward-headers-strategy=NATIVE
  • server.forward-headers-strategy=FRAMEWORK
  • server.tomcat.internal-proxies=.*
  • server.tomcat.remoteip.protocol-header=x-forwarded-proto
  • server.tomcat.remoteip.remote-ip-header=x-forwarded-for

将此添加到路径的 nginx 配置中 -

      resolver 127.0.0.11 valid=30s;
      set $upstream_be application;
      proxy_pass http://$upstream_be:8080;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-Port $server_port;
      proxy_set_header Host $host;

不幸的是,这些都没有奏效,问题仍然存在。有什么想法吗?

相关 pom.xml 版本:spring-boot、spring-security=2.4.5、所有 azure-spring 依赖项=3.4.0

在我们将 azure 依赖项从 2.3.5 升级到 3.4.0 之前,这也运行良好。

【问题讨论】:

    标签: spring spring-security azure-active-directory


    【解决方案1】:

    当代理应用X-Forwarded-ProtoX-Forwarded-Host 等标准RFC7239 "Forwarded Headers" 时,在应用以下两个配置后应该正确计算重定向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 Boot 中如何定义重定向 URL?如果您对基本 URL 使用占位符,例如 {baseUrl}/login/oauth2/code/{registrationId},则上述配置有效。这样,{baseUrl} 占位符由 Spring Security 动态解析,具体取决于它是否在代理后面。

    官方文档中的更多信息:

    【讨论】:

    • 您好 Thomas,感谢您抽出宝贵时间回复。假设我们使用的是 Tomcat,您是正确的。目前我们的 redirect_uri 是这样定义的 - spring.security.oauth2.client.registration.azure.redirect-uri: https://10.10.10.10/v1/login/oauth2/code/ 如果我用 {baseUrl} 替换第一部分,它仍然会解决这个问题还是会在这些更改后解析为 localhost?
    猜你喜欢
    • 2015-07-16
    • 2021-07-11
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 2019-05-03
    • 2010-12-03
    相关资源
    最近更新 更多