简体   繁体   English

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

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

How to redirect my previous page after SSO Login in spring security如何在 spring 安全中 SSO 登录后重定向我的上一页

I used set userReferer as true,我将 userReferer 设置为 true,

But not able achieve it.但无法实现。 please suggest some sample code or site.请建议一些示例代码或网站。

Spring security with IDP we are using我们正在使用 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);
    }
}

Once the user gets authenticated on IDP (Identity provider) side then SP (Service provider) receives assertions or response from IDP.一旦用户在 IDP(身份提供者)端获得身份验证,SP(服务提供者)就会收到来自 IDP 的断言或响应。 That response would be validated on SP side.该响应将在 SP 端进行验证。 Upon response validation, this class OAuthUserLoginSuccessHandler would be called, where you can extract the information from the response provided by IDP and proceed with redirection as per the following code.响应验证后,将调用此 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