简体   繁体   English

Spring Boot社交媒体-Facebook登录

[英]Spring boot social media - facebook login

I'm trying to do facebook login using spring boot social. 我正在尝试使用Spring Boot Social登录Facebook。 To do this I create url from facebook properties. 为此,我从facebook属性创建url。 I have url and when I get it from debug mode and put into browser it's working as I wish (so link is ok), but when I send redirect from spring controller nothing happen. 我有url,当我从调试模式获取它并放入浏览器时,它可以按我的意愿工作(因此链接正常),但是当我从spring控制器发送重定向时,什么都没有发生。 In google chrome I can see that request was send and I can also see such errror in chrome console: 在谷歌浏览器中,我可以看到请求已发送,并且在浏览器控制台中也可以看到这样的错误:

Failed to load [url]. 无法加载[url]。 Response for preflight is invalid (302 status). 飞行前响应无效(302状态)。

My controller method: 我的控制器方法:

@RequestMapping(value = "/connect/facebook", method = RequestMethod.POST)
public String send(Model model){
    String url = socialMediaService.createAuthorizationURL();
    return "redirect:" + url;
}

I know that it's some problem with redirecting to this link but I don't know how to do this in proper way. 我知道重定向到此链接存在一些问题,但我不知道如何以正确的方式进行操作。

EDIT: 编辑:

It was all about CORS „Access-Control-Allow-Origin” attribute. 都是关于CORS的“ Access-Control-Allow-Origin”属性。 It's enough to send this redirect trough javascript with dataType "jsonp", like this: 发送数据类型为“ jsonp”的重定向槽javascript就足够了,就像这样:

$.ajax({
     url: data.url,
     type: "get",
     dataType: "jsonp"
});

The "redirect:" + url approach is intended for the use case where you might normally return a view. "redirect:" + url方法用于通常返回视图的用例。 You can set that view's name to "redirect:" + url and then return it to issue a redirect instead of rendering your view normally. 您可以将该视图的名称设置为"redirect:" + url ,然后将其返回以发出重定向,而不是正常呈现视图。 Since your controller operation is always redirecting, try returning a RedirectView : 由于您的控制器操作始终在重定向,因此请尝试返回RedirectView

@RequestMapping(value = "/connect/facebook", method = RequestMethod.POST)
public RedirectView send(Model model){
    String url = socialMediaService.createAuthorizationURL();
    return new RedirectView(url);
}

Docs for RedirectView RedirectView文件

Additionally, you will want to ensure that you use the correct one of https versus http and that your url is properly constructed. 此外,您将要确保使用正确的httpshttp并正确构建了URL。 For example, https://example.url/resource may not be the same as https://example.url/resource/ 例如, https://example.url/resource可能与https://example.url/resource/

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

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