【问题标题】:ReactJS + Spring Social (Facebook) + Redirect back to React after authenticationReactJS + Spring Social (Facebook) + 身份验证后重定向回 React
【发布时间】:2018-01-07 16:53:56
【问题描述】:

我的后端(弹簧启动)应用程序在 http://localhost:8080 上运行

我的前端(react js)应用程序在 http://localhost:3000 上运行

我的前端登录按钮通过 Facebook (http://localhost:8080/connect/facebook) 进行身份验证,后者通过后端应用程序与 oauth 共舞。这是 spring-social 插件免费提供的。

成功验证后,我将 facebookConnected.html 重定向到 http://localhost:8080/handle-successful-authentication,这是我的后端应用程序中处理验证后逻辑的端点。

处理完这个问题后,如何将控制权交还给我的前端?

【问题讨论】:

  • 前端永远不会将控制权交给后端。后端永远不必手动控制。前端向后端发出请求:它仍然处于控制之中,它可以继续工作,或者它可以选择等到后端完成构建并发送其响应。一旦您的后端控制器返回,您的后端交互就结束了。
  • 你是怎么解决的?
  • @kevcodez 我添加了一个自定义控制器,它按照本文中的建议覆盖重定向调用:littlebigextra.com/…

标签: spring facebook authentication spring-social spring-social-facebook


【解决方案1】:

也许您应该检查 referer 标头文件,看看它是否满足您的需求:在成功登录过程后使用它重定向回来。检查this answer 是否使用SimpleUrlAuthenticationSuccessHandler

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

或者,如果您在 spring 中手动配置更多部分 - 您可以使用 this answer 用于在过滤阶段获取引荐来源网址并将其保存在会话中。 该答案的一种修改可能是:扩展 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);
    }
}

所以你可以在处理你的'...handle-successful-authentication'后重定向 - 但我看到这个重定向是开销,试着把这个逻辑放在其他地方(例如,如果 UserInfoTokenServices 中的successHandler() 或 pricipalExtractor()您需要来自社交 oauth 提供商的更多用户详细信息)

successHandler() 可能如下所示:

@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    相关资源
    最近更新 更多