繁体   English   中英

是否可以在没有服务器端支持的情况下在客户端使用 google recaptcha? 如果可能怎么办?

[英]Is it possible to use google recaptcha on client side without server side support? If possible how?

目前,我正在处理 Nuxtjs 2 项目,我必须在客户端实现 Recaptcha,我不希望后端支持 Recaptcha。 我只想在前端处理它。 在有或没有任何与 nuxtjs2 兼容的库的情况下,我如何在前端执行此操作?

没有服务器就没有理由使用 reCAPTCHA。 reCAPTCHA 用于保护后端免受垃圾邮件的侵害。

如果你真的想这样做,你可以用JavaScript提出同样的要求。

但同样:没有意义。

PHP 的 reCAPTCHA 验证:

function validate_captcha_response($code){
  if($_SERVER['HTTP_HOST']=="localhost") return true;
  if($code && strlen($code)>32){
    $secret = "<your reCaptcha v3 secret>";
    $ip = $_SERVER['REMOTE_ADDR'];
    $gcaptcha = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$code&remoteip=$ip"), true);
    return ($gcaptcha['success'] == true && $gcaptcha['score'] >= 0.8 && $gcaptcha['hostname'] == $_SERVER['SERVER_NAME']);
  }
  return false;
}

JavaScript 中的相同代码:

function validate_captcha_response(code){
  if(window.location.hostname=="localhost") return true;
  if(code && code.length>32){
    let secret = "<your reCaptcha v3 secret>";
    let gcaptcha = json_decode(file_get_contents(`https://www.google.com/recaptcha/api/siteverify?secret=${secret}&response=${code}`), true);
    return (gcaptcha['success'] == true && gcaptcha['score'] >= 0.8 && gcaptcha['hostname'] == window.location.hostname);
  }
  return false;
}

(请注意我删除了 IP 功能)

验证码来源: https://www.custom-captcha.com/

暂无
暂无

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

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