Google验证码reCAPTCHA接入Web

本文详细介绍了如何接入Google的reCAPTCHA服务,包括获取site key,两种接入方式(DOM插入和JS渲染),以及配置参数如onload、render、hl等,确保服务正常运行并显示中文界面。
摘要由CSDN通过智能技术生成

1. 介绍

reCAPTCHA是google旗下的验证码服务(免费的),当然要保证网络能打开google才行,所以常用于国外的网站。如果需要在国内使用,只需将所有的www.google.com替换成www.recaptcha.net即可。
官网:https://developers.google.com/recaptcha/

2. 接入

(1) site key

To start using reCAPTCHA, you need to sign up for an API key pair for your site. The key pair consists of a site key and secret key. The site key is used to invoke reCAPTCHA service on your site or mobile application. The secret key authorizes communication between your application backend and the reCAPTCHA server to verify the user’s response. The secret key needs to be kept safe for security purposes.

申请Site Key: http://www.google.com/recaptcha/admin
Verifying the user’s response:
https://developers.google.com/recaptcha/docs/verify

本地开发时,可以使用此site key进行测试:
6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI
这里写图片描述

(2) 方式一:DOM中加入

<html>
  <head>
    <title>reCAPTCHA demo</title>
     <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  </head>
  <body>
    <form action="?" method="POST">
      <div class="g-recaptcha" data-callback="robotVerified" data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"></div>
    </form>
  </body>
  <script>
    function robotVerified(){
    
      console.log('Verified: not robot');
    }
  </script>
</html>

(3) 方式二:Js 渲染

<html>
  <head>
    <title>reCAPTCHA demo</title>
    <script src="https://www.google.com/recaptcha/api.js?onloadCallback&render=explicit" async defer></script>
  </head>
  <body>
    <div id="robot"></div>
    <script type="text/javascript">
      var onloadCallback = function() {
    
        console.log("grecaptcha is ready!");
        //render grecaptcha
        grecaptcha.render('robot', {
    
          'sitekey': '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI',
          'callback': function (</
  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值