v2的版本
1. 介紹
reCAPTCHA是google旗下的驗證碼服務(wù)(免費(fèi)的)厦幅,當(dāng)然要保證網(wǎng)絡(luò)能打開google才行,所以常用于國外的網(wǎng)站慨飘。如果需要在國內(nèi)使用确憨,只需將所有的www.google.com替換成www.recaptcha.net即可。
官網(wǎng):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
在本地開發(fā)時瓤的,可以使用此site key進(jìn)行測試:
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 () {
? ? ? ? ? ? console.log('Verified: not robot');
? ? ? ? ? }
? ? ? ? });
? ? ? };
? ? </script>
? </body>
</html>
3. 配置
(1) 引用api.js的url參數(shù)
<script src="https://www.google.com/recaptcha/api.js?onloadCallback&render=explicit" async defer></script>
在引用recaptcha的api時休弃,url上可以配置一些參數(shù),如下:
參數(shù)名稱? ? 是否可選? ? 值? ? 描述
onload? ? 是? ? 函數(shù)名稱? ? 定義加載完成的回調(diào)函數(shù)名稱
render? ? 是? ? explicit 或 onload(默認(rèn))? ? 當(dāng)api加載好之后是否直接在類名為g-recaptcha的標(biāo)簽上渲染recaptcha圈膏。默認(rèn)的onload是要渲染塔猾,explicit是不渲染
hl? ? 是? ? 點(diǎn)擊查詢不同語言對應(yīng)的值? ? recaptcha顯示語言,如果未指定稽坤,自動檢測用戶的語言丈甸。
[1] onload
<html>
? <head>
? ? <title>reCAPTCHA demo</title>
? ? <script src="https://www.google.com/recaptcha/api.js?onloadCallback" async defer></script>
? </head>
? <body>
? ? <form action="?" method="POST">
? ? ? <div class="g-recaptcha"data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"></div>
? ? </form>
? </body>
? <script>
? ? function onloadCallback(){
? ? ? console.log("grecaptcha is ready!");
? ? }
? </script>
</html>
[2] render
設(shè)置為explicit后,api加載完成以后也不會自動渲染rechaptcha框尿褪,頁面會顯示空白睦擂。
<html>
? <head>
? ? <title>reCAPTCHA demo</title>
? ? <script src="https://www.google.com/recaptcha/api.js?render=explicit" async defer></script>
? </head>
? <body>
? ? <form action="?" method="POST">
? ? ? <div class="g-recaptcha"data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"></div>
? ? </form>
? </body>
</html>
[3] hl
將hl配置為zh-CN后,顯示簡體中文界面杖玲。
<html>
? <head>
? ? <title>reCAPTCHA demo</title>
? ? <script src="https://www.google.com/recaptcha/api.js?hl=zh-CN" async defer></script>
? </head>
? <body>
? ? <form action="?" method="POST">
? ? ? <div class="g-recaptcha"data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"></div>
? ? </form>
? </body>
</html>
(2) reCAPTCHA自定義參數(shù)
在上文的接入章節(jié)中提到了顿仇,有兩種接入的方式,兩種方式都可以進(jìn)行自定義配置摆马。
類名為g-recaptcha的標(biāo)簽上的屬性? ? grecaptcha.render參數(shù)? ? 必填? ? 值? ? 描述
data-sitekey? ? sitekey? ? 是? ? ? ? 網(wǎng)站sitekey
data-theme? ? theme? ? ? ? dark或light(默認(rèn))? ? 主題顏色
data-size? ? size? ? ? ? compact或normal(默認(rèn))? ? 框大小
data-tabindex? ? tabindex? ? ? ? 默認(rèn)為0? ? The tabindex of the widget and challenge. If other elements in your page use tabindex, it should be set to make user navigation easier.
data-callback? ? callback? ? ? ? 驗證成功回調(diào)函數(shù)? ? g-recaptcha-response token會作為參數(shù)傳遞到回調(diào)函數(shù)中
data-expired-callback? ? expired-callback? ? ? ? 驗證失效回調(diào)函數(shù)? ? 用戶需要重新驗證
data-error-callback? ? error-callback? ? ? ? 錯誤回調(diào)函數(shù)? ? 因為網(wǎng)絡(luò)等問題無法驗證臼闻,通過回調(diào)函數(shù)提醒用戶重試
[1] theme
dark和light如下
[2] size
compact和normal如下:
[3] data-callback
<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(data){
? ? ? console.log(data);
? ? }
? </script>
</html>
在回調(diào)函數(shù)robotVerified中接受到的這個參數(shù)就是g-recaptcha-response token(注意:每次都不一樣)。如下:
03AHqfIOks78_xzIktpZlVUGhCIiRruEDFaP9aAir-ChZpGbSdQ3JcUpCuEQ9KwuynC5M-TR8wQfppmIJqIXBGl1xvusZqj444QqI2dhyTXx6LmL9y00knaFIRn5KC9ILWX-H3Hgv66mamhakmo3x1EIEoiA03EixOtPgbW_Q68dDl48B0_8qHl6o9KOfLiolqdwIApi61FZCqUXhvPKZc55i2FVA2qvsh0oauN0YdHWG_ReyXlW2vdS-A0U0E28NWafM1-IfDnxOBchxSE-uYIiXB4QeruyeNaA
(3) JavaScript API
方法? ? 參數(shù)? ? 返回? ? 描述
grecaptcha.render(container,parameters)? ? 【container】id名稱(字符串) or document.getElementById(‘example1’)
【parameters】參照上節(jié)內(nèi)容囤采,如{"sitekey": "your_site_key", "theme": "light"}? ? widget id? ? 在container指定的標(biāo)簽內(nèi)渲染reCAPTCHA模塊
grecaptcha.reset(opt_widget_id)? ? widget id(可選述呐,省略時默認(rèn)為第一個創(chuàng)建的模塊)? ? -? ? 重置reCAPTCHA
grecaptcha.getResponse(opt_widget_id)? ? widget id(可選,省略時默認(rèn)為第一個創(chuàng)建的模塊)? ? 【未通過】""
【已通過】g-recaptcha-response token? ? 獲得reCAPTCHA的結(jié)果響應(yīng)
[1] render
<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="robot1"></div>
? ? <div id="robot2"></div>
? ? <script type="text/javascript">
? ? ? var onloadCallback = function() {
? ? ? ? var widge1 = grecaptcha.render('robot1', {
? ? ? ? ? 'sitekey': '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI',
? ? ? ? ? theme: 'dark'
? ? ? ? });
? ? ? ? console.log(widge1); //0
? ? ? ? var widget2 = grecaptcha.render(document.getElementById('robot2'), {
? ? ? ? ? 'sitekey': '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI',
? ? ? ? ? tabindex:'2'
? ? ? ? });
? ? ? ? console.log(widget2); //1
? ? ? };
? ? </script>
? </body>
</html>
[2] reset
<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>
? ? <button onclick="resetRecaptcha()">重置</button>
? ? <script type="text/javascript">
? ? ? var widgeId;
? ? ? var onloadCallback = function() {
? ? ? ? widgeId = grecaptcha.render('robot', {
? ? ? ? ? 'sitekey': '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI',
? ? ? ? });
? ? ? };
? ? ? function resetRecaptcha(){
? ? ? ? grecaptcha.reset(widgeId); // <---看這里
? ? ? }
? ? </script>
? </body>
</html>
[3] getResponse
<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>
? ? <button onclick="getResponseFromRecaptcha()">看看驗證通過了沒蕉毯?</button>
? ? <script type="text/javascript">
? ? ? var widgetId;
? ? ? var onloadCallback = function() {
? ? ? ? widgetId = grecaptcha.render('robot', {
? ? ? ? ? 'sitekey': '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI',
? ? ? ? });
? ? ? };
? ? ? function getResponseFromRecaptcha(){
? ? ? ? var response = grecaptcha.getResponse(widgetId); // <---看這里
? ? ? ? if (response.length == 0) { //返回""或 token(如:03AHqfIOmtBMGg686uP3YPAN7hJyaqUi69SSrEnC3Km7Hcnr5y1u-A-yp8z28yBvgVxqWiRndLrXrk_TyJrdob3tVh4BGnI4YNsoKocqZmbilGnJRX6Mxxj6jQ0AkhoRangAVFmRFZyy7RH5u5ruM5t_H7-P4GNnaFUaMELgOYyJ5ZgUtEMwBS1iNrGg6B6MZmgd4kcJXhWYR4vU_RwqFOuzxd9NMF7OCLl8FzOhN4baI5S2krnORqMBkiWenKNUtCuKfB1HzGdiVog7mzBKJvseSBlAPXrTN-Gg)
? ? ? ? ? console.log('reCaptcha not verified');
? ? ? ? ? return false;
? ? ? ? } else {
? ? ? ? ? console.log('reCaptcha verified');
? ? ? ? ? return true;
? ? ? ? }
? ? ? }
? ? </script>
? </body>
</html>