0

我有我的後端(春季啓動)應用程序上http://localhost:8080ReactJS +彈簧社會(臉譜)+重定向回驗證

運行我有我的前端後反應(反應JS)上http://localhost:3000

運行的應用程序我前端SignIn按鈕通過後臺應用程序與oauth舞蹈的Facebook進行認證(http://localhost:8080/connect/facebook)。這是通過spring-social插件免費提供的。

成功進行身份驗證後,我將facebookConnected.html重定向到http://localhost:8080/handle-successful-authentication,這是我的後端應用程序中用於處理驗證後邏輯的端點。

一旦我處理了這個問題,我該如何將控制權交還給我的前端?

+0

前端永遠不會將控制權交給後端。後端永遠不需要手動控制。前端向後端發出請求:它仍然在控制之中,它可以繼續工作,或者可以選擇等到後端完成構建併發送響應。一旦您的後端控制器返回,這是您的後端交互的結束。 –

+0

你是如何解決它的? – kevcodez

+0

@kevcodez我添加了一個自定義控制器來覆蓋重定向調用,如本文中所建議的:http://www.littlebigextra.com/how-to-change-the-default-spring-social-redirect-flow/。 – alphathesis

回答

0

也許你應該檢查referer頭文件提交,看看它是否能滿足你的需求:成功登錄過程後使用它重定向回來。檢查this answer使用SimpleUrlAuthenticationSuccessHandler

@Bean 
public AuthenticationSuccessHandler successHandler() { 
SimpleUrlAuthenticationSuccessHandler handler = new SimpleUrlAuthenticationSuccessHandler(); 
    handler.setUseReferer(true); 
    return handler; 
} 

或者,如果你在手動配置春季多件 - 您可以使用this answer 用於獲取引用者URL過濾器相併將其保存在會話中。 一個修飾,其答案可能是:延長OAuth2ClientAuthenticationProcessingFilter和的doFilter得到引薦值

public class MyOAuth2ClientAuthenticationProcessingFilter extends OAuth2ClientAuthenticationProcessingFilter { 
... 
@Override 
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 
     throws IOException, ServletException { 
    HttpServletRequest request = (HttpServletRequest) req; 
    String referrer = request.getHeader("Referer");   
    if (null != referrer) 
     request.getSession().setAttribute("url_prior_login", referrer); 

    super.doFilter(req, res, chain); 
    } 
} 

這樣你就可以處理。第您的「...處理成功的認證」後重定向 - 但我看到這個重定向是開銷,儘量把這種邏輯在其他地方

successHandler()看起來是這樣的(如successHandler()或pricipalExtractor()在UserInfoTokenServices如果你需要從社會的OAuth提供更多用戶詳細信息。):

@Bean 
public AuthenticationSuccessHandler successHandler() { 
    AuthenticationSuccessHandler rst = new AuthenticationSuccessHandler() { 
    @Override 
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, 
       Authentication authentication) throws IOException, ServletException { 
      ... 
      HttpSession session = request.getSession(); 
      String redirectUrl = null; 
      if (session != null) { 
       redirectUrl = (String) session 
         .getAttribute("url_prior_login"); 

      if (null == redirectUrl || redirectUrl.trim().length() <= 0) 
       redirectUrl = "http://your_default_redirect_url"; 

      response.sendRedirect(redirectUrl); 
     }; 
     return rst; 
} 

無論如何,支票彈簧docs