我已经创建了Spring作为后端,并为Google身份验证启用了社交登录。 /signin/google
是具有方法POST
的终结点,并且内容类型为application/x-www-form-urlencoded
,其scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email
。
如果我从邮递员客户端(Google chrome应用程序)调用上述端点,则邮递员客户端可以很好地工作,它为我提供了200个状态代码和JSESSIONID,并且我能够调用其他安全api。
但是对于本机反应我无法执行它。 帮助将不胜感激。 下面提到的是我用来触发Google登录的功能。
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));
};
它总是为我提供CORS错误政策。 下面提到的是后端的cors配置。
allowed-origins: "*"
allowed-methods: "*"
allowed-headers: "Access-Control-Allow-Headers, Authorization, Access-Control-Allow-Headers, Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers"
exposed-headers: "Authorization,Link,X-Total-Count"
allow-credentials: true
max-age: 1800
这是一个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));
});