簡體   English   中英

如何在 spring 安全 SSO 登錄后重定向我的上一頁?

[英]How to redirect my previous page after SSO Login in spring security?

如何在 spring 安全中 SSO 登錄后重定向我的上一頁

我將 userReferer 設置為 true,

但無法實現。 請建議一些示例代碼或網站。

我們正在使用 IDP 的 Spring 安全性

public class LoginSuccessHandler extends SimpleUrlAuthenticationSuccessHandler
        implements AuthenticationSuccessHandler {

    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

    public LoginSuccessHandler() {
        super();
        setUseReferer(true);
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {

        /// some code 

        //set our response to OK status
        httpServletResponse.setStatus(HttpServletResponse.SC_OK);

        String targetUrl = determineTargetUrl(authentication);

        httpServletResponse.sendRedirect(targetUrl);
    }
}

一旦用戶在 IDP(身份提供者)端獲得身份驗證,SP(服務提供者)就會收到來自 IDP 的斷言或響應。 該響應將在 SP 端進行驗證。 響應驗證后,將調用此 class OAuthUserLoginSuccessHandler ,您可以在其中從 IDP 提供的響應中提取信息,並按照以下代碼繼續進行重定向。

  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

  public class OAuthUserLoginSuccessHandler extends 
      SavedRequestAwareAuthenticationSuccessHandler {
   public OAuthUserLoginSuccessHandler() {}

   @Override
    public void onAuthenticationSuccess(final HttpServletRequest request,
     final HttpServletResponse response, final Authentication authentication)
     throws IOException, ServletException {

    if (authentication.getPrincipal() instanceof UserDetails
         || authentication.getDetails() instanceof UserDetails) {
    UserDetails details;

    if (authentication.getPrincipal() instanceof UserDetails) {
      details = (UserDetails) authentication.getPrincipal();
    } else {
      details = (UserDetails) authentication.getDetails();
    }

    String username = details.getUsername();
    // get user info from datastore using username 
    // some code 
 
    String redirectUri;  // get target uri either from relay state or from datastore
    if (null != redirectUri) {
       response.sendRedirect(redirectUri);
       return;
     }
   }
   super.onAuthenticationSuccess(request, response, authentication);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM