Ani*_*aje 4 javascript recaptcha cors angularjs preflight
我正在使用 AngularJS 并尝试使用 Google 的 reCAPTCHA,我正在使用“显式呈现 reCAPTCHA 小部件”方法在我的网页上显示 reCAPTCHA,
HTML 代码 -
<script type="text/javascript">
var onloadCallback = function()
{
grecaptcha.render('loginCapcha', {
'sitekey' : 'someSiteKey',
'callback' : verifyCallback,
'theme':'dark'
});
};
var auth='';
var verifyCallback = function(response)
{
//storing the Google response in a Global js variable auth, to be used in the controller
auth = response;
var scope = angular.element(document.getElementById('loginCapcha')).scope();
scope.auth();
};
</script>
<div id="loginCapcha"></div>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>
Run Code Online (Sandbox Code Playgroud)
到目前为止,我能够实现用户是人类还是机器人所需的功能,
根据我上面的代码,我的代码中有一个名为“verifyCallback”的回调函数,
它存储由 Google 创建的响应,在名为“auth”的全局变量中。
现在,reCAPCHA 的最后一部分是调用 Google API,
使用“ https://www.google.com/recaptcha/api/siteverify ”作为 URL 并使用POST方法,
并将密钥和创建的响应传递给它由 Google 提供,我已在下面的代码中完成。
我的控制器 -
_myApp.controller('loginController',['$rootScope','$scope','$http',
function($rootScope,$scope,$http){
var verified = '';
$scope.auth = function()
{
//Secret key provided by Google
secret = "someSecretKey";
/*calling the Google API, passing it the Secretkey and Response,
to the specified URL, using POST method*/
var verificationReq = {
method: 'POST',
url: 'https://www.google.com/recaptcha/api/siteverify',
headers: {
'Access-Control-Allow-Origin':'*'
},
params:{
secret: secret,
response: auth
}
}
$http(verificationReq).then(function(response)
{
if(response.data.success==true)
{
console.log("Not a Bot");
verified = true;
}
else
{
console.log("Bot or some problem");
}
}, function() {
// do on response failure
});
}
Run Code Online (Sandbox Code Playgroud)
所以,我实际面临的问题是我无法访问 Google 的 URL,
以下是我发送的请求和错误的屏幕截图。
据我了解,它与 CORS 和 Preflight request 相关。
那么我做错了什么?我该如何解决这个问题?
如谷歌文档中所述https://developers.google.com/recaptcha/docs/verify
本页面介绍了如何验证用户对来自应用程序后端的 reCAPTCHA 质询的响应。
验证是从服务器发起的,而不是客户端。
这是服务器的额外安全步骤,以确保来自客户端的请求是合法的。否则,客户端可能会伪造响应,而服务器会盲目地相信客户端是经过验证的人。
| 归档时间: |
|
| 查看次数: |
6960 次 |
| 最近记录: |