代码之家  ›  专栏  ›  技术社区  ›  Alex L

Post请求后从localhost:8080重定向到localhost:3000

  •  2
  • Alex L  · 技术社区  · 8 年前

    我正在尝试向我的后端提交一个表单提交post请求,以使用Facebook进行用户身份验证,我被重定向到Facebook并登录到Facebook。我已成功验证,并返回了用户对象(在我的后端中配置)。我的后端Facebook身份验证是按照 spring social tutorial

    const auth_url = "/connect/facebook";
    <form className="signin__form" action={auth_url} method="post">
        <input type="hidden" name="scope" value="user_friends" />
        <button type="submit" className="signin__form-btn">
        SIGN IN WITH FACEBOOK
        </button>
    </form>
    

    我的包裹。json将connect/facebook响应代理给localhost:8080,如下所示:

    {
    ...
    "proxy": {
        "/connect": {
          "target": "http://localhost:8080",
          "secure": false,
          "changeOrigin": true
        }
      },
    ...
    }
    

    我希望看到的是:

    {React view components}
    

    在本地主机上:8080

    {
      user: info
    }
    

    我的问题是如何返回localhost:3000在后端重定向完成后,我是否应该在后端使用另一种方法,这样我就可以让ajax在前端获取用户信息的请求,而不是返回JSON对象。我不知道如何在春季后重定向回3000。社交握手结束。

    1 回复  |  直到 8 年前
        1
  •  2
  •   StanislavL    8 年前

    实际上你可以换控制器

    @RequestMapping(value = "/auth_url", method = RequestMethod.POST)
    public void login(HttpServletResponse response) {
        response.setHeader("Location", "http://localhost:3000");
    }
    

    @RequestMapping(value = "/auth_url", method = RequestMethod.POST)
    public ModelAndView login() {
            return new ModelAndView("redirect:http://localhost:3000");
    }
    

    或者,我建议通过AJAX提交表单并在不离开localhost:3000的情况下处理onSuccess