我正在使用 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”,因此 redirect-uri 不再相等(当 localhost 不是正确的主机名时)。
如何获取 authorizationResponse.getRedirectUri()
和 authorizationRequest.getRedirectUri()
的值?我想看看 Spring Security 看到了哪个重定向 uri。调试控制台不打印这些值。
最佳答案
我创建了一个打印出 request.getRequestURL().toString() 的端点,实际上它打印了原始 url 而不是反向代理 url。这可能是问题所在。
关于spring-boot - 如何在 Spring Security oauth2 中调试重定向 uris?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54438597/
相关文章:
python - 如何在 django 中使用 ajax 形式验证 django-recaptcha
javascript - 为什么 Chrome 在使用 Typed.js 时难以呈现表情符号?
python - 如何在 Mac 上将 Python 完全恢复为出厂设置
typo3 - 使用流体创建数组和对象 f :variable
python-3.x - celery 错误 : result. get(timeout=5) 超时
angular - 如何在 ngrx 效果中使用 LatestFrom 进行单元测试
sql - AWS Redshift 身份列插入 - 新记录的身份值不大于以前的最大身份值