Jhipster Spring后端-社交登录和React Native前端

时间:2019-08-16 08:00:11

标签: spring-boot react-native jhipster google-login

我已将Spring创建为后端,并为Google身份验证启用了社交登录。 /signin/google是具有方法POST的端点,内容类型是具有application/x-www-form-urlencoded的{​​{1}}。

如果我从邮递员客户端(Google chrome应用程序)调用上述端点,邮递员客户端可以很好地工作,它为我提供200个状态代码和 JSESSIONID ,并且我能够调用其他安全api。

但是为了响应本机,我无法执行它。帮助将不胜感激。下面提到的是我用来触发Google登录的功能。

scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email

它始终为我提供CORS错误政策。下面提到的是后端的cors配置。

googleSignin = () => {
      var data = 'scope=' + encodeURIComponent('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email');
      axios({
        url: baseUrl + 'signin/google',
        method: 'POST',
        data: data,
        config: {
          headers:
          {
            'cache-control': 'no-cache',
            'content-type': 'application/x-www-form-urlencoded',
            'Access-Control-Allow-Credentials': true,
            'Access-Control-Allow-Origin': true
          },
          credentials: "same-origin"
        },
        withCredentials: true
      })
        .then(res => {
          console.log('googleSignin res() ---> ', res.headers);

        })
        .catch(e => console.log(e));
    };

和屏幕截图是错误: enter image description here

1 个答案:

答案 0 :(得分:0)

这是一个cross-domain问题。在jQuery 1.5.0及更高版本之后,cross-domain被阻止。结果,当ajax请求时发生以下错误:

尝试此代码。

const options = baseUrl + 'signin/google'



$.ajaxPrefilter( function (options) {

  if (options.crossDomain && jQuery.support.cors) {

    var http = [removed].protocol === 'http:' ? 'http:' : 'https:');

    options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;

  }

});



$.post(

options,

    function (response) {

        console.log(">>>> " + JSON.stringify(response));

});