【发布时间】:2019-06-23 14:47:33
【问题描述】:
我正在使用 Spring Security 在我的 Spring Boot Web 应用程序中设置 oauth2 身份验证,该应用程序在反向代理后面运行。
流程:
1) 导航到我的 Spring 应用的登录页面
2) 重定向到 oauth 提供者。登录。
3) 重定向回我的 redirect-uri 端点
4) 流程中断和默认 Spring 登录页面打印:
Your login attempt was not successful, try again.
Reason: [invalid_redirect_uri_parameter]
我打开调试,控制台显示:
2019-01-30 09:46:40.094 DEBUG 1 --- [nio-8080-exec-2] o.s.s.authentication.ProviderManager : Authentication attempt using org.springframework.security.oauth2.client.authentication.OAuth2LoginAuthenticationProvider
2019-01-30 09:46:40.094 DEBUG 1 --- [nio-8080-exec-2] o.s.s.authentication.ProviderManager : Authentication attempt using org.springframework.security.oauth2.client.oidc.authentication.OidcAuthorizationCodeAuthenticationProvider
2019-01-30 09:46:40.095 DEBUG 1 --- [nio-8080-exec-2] .s.o.c.w.OAuth2LoginAuthenticationFilter : Authentication request failed: org.springframework.security.oauth2.core.OAuth2AuthenticationException: [invalid_redirect_uri_parameter]
org.springframework.security.oauth2.core.OAuth2AuthenticationException: [invalid_redirect_uri_parameter]
at org.springframework.security.oauth2.client.oidc.authentication.OidcAuthorizationCodeAuthenticationProvider.authenticate(OidcAuthorizationCodeAuthenticationProvider.java:136) ~[spring-security-oauth2-client-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
深入OidcAuthorizationCodeAuthenticationProvider.java的代码发现下面的语句触发了错误:(authorizationResponse.getRedirectUri().equals(authorizationRequest.getRedirectUri()))
我检查了浏览器请求。我的初始请求的重定向 uri 参数等于我在提供商的站点进行身份验证后重定向的站点。 (否则提供商会抛出重定向 uri 不匹配错误)。
根据另一位用户的经验 (http://blog.davidvassallo.me/2018/08/10/lessons-learnt-of-spring-boot-oauth2-redirect-uris/),Spring Boot 在反向代理后面运行时会看到“localhost”,因此重定向 uri 不再相等(当 localhost 不是正确的主机名时)。
如何获取authorizationResponse.getRedirectUri() 和authorizationRequest.getRedirectUri() 的值?我想看看 Spring Security 看到了哪个重定向 uri。调试控制台不打印这些值。
【问题讨论】:
标签: spring-boot spring-security reverse-proxy spring-security-oauth2