简体   繁体   English

ReactJS + Spring Social(Facebook)+身份验证后重定向回React

[英]ReactJS + Spring Social (Facebook) + Redirect back to React after authentication

I have my backend (spring boot) application running on http://localhost:8080 我的后端(春季启动)应用程序在http:// localhost:8080上运行

I have my frontend (react js) application running on http://localhost:3000 我的前端(反应式js)应用程式在http:// localhost:3000上执行

My front-end SignIn button authenticates with Facebook ( http://localhost:8080/connect/facebook ) which does the oauth dance with by backend application. 我的前端SignIn按钮通过Facebook( http:// localhost:8080 / connect / facebook )进行身份验证,后者通过后端应用程序与oauth进行舞蹈。 This is provided for free with the spring-social plugin. spring-social插件免费提供此功能。

After successfully authenticating, I have facebookConnected.html redirect to http://localhost:8080/handle-successful-authentication which is an endpoint in my backend application that handles post-authentication logic. 成功进行身份验证后,我将facebookConnected.html重定向到http:// localhost:8080 / handle-successful-authentication ,这是我的后端应用程序中处理身份验证后逻辑的终结点。

Once I handle this, how do I hand control over back to my frontend? 处理完这些后,如何将控制权交还给我的前端?

Maybe you should check referer header filed and see if it can fit your needs: use it to redirect back after successful login process. 也许您应该检查引荐来源标头字段,并查看它是否满足您的需求:成功登录后,使用它重定向回。 Check this answer for using SimpleUrlAuthenticationSuccessHandler 检查此答案以使用SimpleUrlAuthenticationSuccessHandler

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

Or if you are configuring more pieces in spring manually - you can use this answer for getting referer url in filter phase and save it in session. 或者,如果您要在春季手动配置更多零件,则可以使用此答案在过滤器阶段获取引荐来源网址并将其保存在会话中。 One modification of that answer could be: extends OAuth2ClientAuthenticationProcessingFilter and in doFilter get referer value 对该答案的一种修改可能是:扩展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);
    }
}

so you can redirect after procesing your '...handle-successful-authentication' - but as I see this redirect is overhead, try to put this logic somewhere else (eg. successHandler() or pricipalExtractor() in UserInfoTokenServices if you need more user details from social oauth provider) 因此您可以在进行“ ...句柄成功身份验证”后进行重定向-但正如我看到的那样,这种重定向是开销,请尝试将此逻辑放在其他地方(例如,如果需要更多,请在UserInfoTokenServices中将successHandler()或pricipalExtractor()放在其他位置)来自社交oauth提供商的用户详细信息)

successHandler() could look like this: 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;
}

Anyway, check spring docs 无论如何,请查看Spring 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM