使用谷歌reCaptcha(2.0版本)做人机验证

百度百科:

CMU设计了一个名叫reCAPTCHA的强大系统,让他们的电脑去向人类求助。具体做法是:将OCR软件无法识别的文字扫描图传给世界各大网站,用以替换原来的验证码图片;那些网站的用户在正确识别出这些文字之后,其答案便会被传回CMU。

使用前需注意: 
1.reCaptcha官网网站为:https://developers.google.com/recaptcha/(需要翻墙)
2.在国内使用的话,需要将demo中所有的www.google.com替换成www.recaptcha.net不然无法使用reCAPTCHA
3.使用reCaptcha需要去注册google账号,并且去https://www.google.com/recaptcha/admin里面去创建秘钥对()稍等我会标注出来)

reCaptcha方式选择:
1.显示
2.隐式
3.Android

实践第一种(显示):
1.创建google账号,访问https://www.google.com/recaptcha/admin创建秘钥对


2.前端:

  1. <!DOCTYPE html>
  2. <html lang=“en”>
  3. <head>
  4. <title></title>
  5. <meta charset=“utf-8”>
  6. <meta name=“viewport” content=“width=device-width, initial-scale=1”>
  7. <script src=‘https://www.recaptcha.net/recaptcha/api.js’></script>
  8. </head>
  9. <body>
  10. <form action=“/check” method=“post”>
  11. <!– 公钥 –>
  12. <div class=“g-recaptcha” data-sitekey=“6Ldnn3cUAAAAANS1T-9rfBL7z6lDnaZj5RXdhApc”></div>
  13. <p><button class=“btn btn-primary” type=“submit”>Register</button>
  14. </form>
  15. </body>
  16. </html>

3.服务端

  1. @RequestMapping(“/check”)
  2. @ResponseBody
  3. public String check(HttpServletRequest request) {
  4. String checkCode = request.getParameter(“g-recaptcha-response”);
  5. Map<String, Object> map = new HashMap<>();
  6. // 私钥
  7. map.put(“secret”, “6Ldnn3cUAAAAADcNDxCOnw_oBV_k0JsvdBMF-KEI”);
  8. map.put(“response”, checkCode);
  9. String json = MyHttpRequest.sendPost(“https://www.recaptcha.net/recaptcha/api/siteverify”, map, “UTF-8”);
  10. return json;
  11. }

实践第二种(隐式):
1.去创建秘钥对,步骤一样,只不过是这里选择项,选择改成第二个了,然后获取新的秘钥对


2.前端

  1. <html>
  2. <head>
  3. <title>reCAPTCHA demo: Simple page</title>
  4. <script src=“https://www.recaptcha.net/recaptcha/api.js” async defer></script>
  5. <script>
  6. function onSubmit(token) {
  7. document.getElementById(“demo-form”).submit();
  8. }
  9. </script>
  10. </head>
  11. <body>
  12. <form id=‘demo-form’ action=“/check2” method=“POST”>
  13. <!– data-sitekey 需要填写公钥 –>
  14. <button class=“g-recaptcha” data-sitekey=“6LfcoXcUAAAAAC0jA5m50z4C0a7Zggrk6sUvgVKs” data-callback=‘onSubmit’>Submit</button>
  15. <br/>
  16. </form>
  17. </body>
  18. </html>

3.服务端

  1. @RequestMapping(“/check2”)
  2. @ResponseBody
  3. public String check2(HttpServletRequest request) {
  4. String checkCode = request.getParameter(“g-recaptcha-response”);
  5. Map<String, Object> map = new HashMap<>();
  6. // 私钥
  7. map.put(“secret”, “6LfcoXcUAAAAAE-G2qDI19ZR5r96sY_f5i6mVWNi”);
  8. map.put(“response”, checkCode);
  9. String json = MyHttpRequest.sendPost(“https://www.recaptcha.net/recaptcha/api/siteverify”, map, “UTF-8”);
  10. return json;
  11. }

第一种效果:

 第二种效果:

服务端调用https://www.recaptcha.net/recaptcha/api/siteverify,返回来的错误码:

Error code Description
missing-input-secret The secret parameter is missing.
invalid-input-secret The secret parameter is invalid or malformed.
missing-input-response The response parameter is missing.
invalid-input-response The response parameter is invalid or malformed.
bad-request The request is invalid or malformed.

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注