为不泄露 secret 信息,供应商将替换为$PROVIDER
.
授权有效,但不是将我重定向到索引,而是将我重定向到 /error
.
重现步骤
http://localhost/oauth_login
显示链接 login
. login
(链接到 http://localhost/oauth2/authorization/$PROVIDER
)http://localhost/error
错误页面显示以下内容(我对其进行了格式化以提高可读性):
{
"timestamp":"2018-04-05T14:18:47.720+0000",
"status":999,
"error":"None",
"message":"No message available"
}
这显然与默认白标签错误页面上显示的参数相同,所以实际上,唯一的问题是它是 JSON,除了是一个 HTML 页面。如果我刷新
http://localhost/error
之后,它向我显示了正常的白标错误页面。现在这就是奇怪的地方,如果我尝试导航到
http://localhost/
重定向到错误页面后,我通过了身份验证(用户数据在那里,所以授权成功)。基本上,问题是我被重定向到 http://localhost/error
而不是 http://localhost/
.因为它功能齐全(除了重定向),我不会发布整个安全配置,而是将其限制为相关代码:
安全配置
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.csrf().disable()
.authorizeRequests().anyRequest()
.authenticated()
.and()
.oauth2Login()
.loginPage("/oauth_login")
.defaultSuccessUrl("/")
.failureUrl("/oauth_login")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/oauth_login").permitAll()
;
}
相关属性
spring.security.oauth2.client.registration.$PROVIDER.redirectUriTemplate=http://localhost/login/oauth2/code/$PROVIDER
相关信息
TL;博士 授权有效但重定向到
/error
而不是 /
.
最佳答案
我实际上找到了我自己问题的答案,但我还是决定发布它,以防有人遇到同样的问题。
.defaultSuccessUrl("/")
本来应该
.defaultSuccessUrl("/", true)
如果您不将其设置为
true
,它会自动将用户重定向到上一个请求,在我的例子中,最后一个请求是向 url /error
发出的。 ,这就解释了我的问题。通过将其设置为
true
,你强制用户重定向到任何你的 defaultSuccessUrl
是。通过查看日志,您实际上可以看到它:
2018-04-05 11:04:09 DEBUG [-nio-80-exec-10] RequestAwareAuthenticationSuccessHandler : Redirecting to DefaultSavedRequest Url: http://localhost/error
2018-04-05 11:04:09 DEBUG [-nio-80-exec-10] o.s.security.web.DefaultRedirectStrategy : Redirecting to 'http://localhost/error'
关于尽管成功,Spring Security OAuth2 登录重定向到错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49675747/