簡體   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