繁体   English   中英

Spring-Security-Oauth2:默认登录成功url

[英]Spring-Security-Oauth2: Default login success url

是否可以为 Spring Oauth2 Sso 服务设置默认登录成功网址?

以下场景

  1. 浏览器请求index.html
  2. sso 服务:不受保护 ==> 返回index.html
  3. index.html 包含manifest属性 ==> 浏览器请求清单
  4. sso 服务:清单受保护 ==> 返回 401
  5. 客户端重定向到${sso.host}/login
  6. sso 服务重定向到 auth 服务器
  7. 身份验证 ==> 使用查询字符串中的代码重定向回${sso.host}/login
  8. sso 服务:请求令牌并重定向到清单文件

有没有办法不重定向到上次请求的受保护资源,而是默认重定向到“index.html”?

即使没有办法实现这一点,也请告诉我

我(我认为)有类似的问题:在我的情况下,一旦SSO请求成功,用户就被重定向到/,这不是我想要的。

有一个内置的解决方案需要一些挖掘才能找到。

AbstractAuthenticationProcessingFilter有一个方法setAuthenticationSuccessHandler ,允许您控制它,因此如果您有权访问OAuth2ClientAuthenticationProcessingFilter您可以将其设置为您想要的。

如果您有类似于教程的设置: httpsOAuth2ClientAuthenticationProcessingFilter ,那么您只需将以下内容添加到教程中创建的OAuth2ClientAuthenticationProcessingFilter

OAuth2ClientAuthenticationProcessingFilter oauth2Filter = new OAuth2ClientAuthenticationProcessingFilter("/XXXProvider/login");
oauth2Filter.setAuthenticationSuccessHandler(new SimpleUrlAuthenticationSuccessHandler() {
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        this.setDefaultTargetUrl("/my_preferred_location");
        super.onAuthenticationSuccess(request, response, authentication);
    }
});

有没有办法不重定向到上次请求的受保护资源,而是默认重定向到“index.html”?

是的,在 WebSecurityConfigurerAdapter 中:

public class MyWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

[...]

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
[...]
            .oauth2Login()
            .defaultSuccessUrl("index.html", true)
[...]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM