zoukankan      html  css  js  c++  java
  • 【原创随笔】强大的谷歌开源免费验证码reCAPTCHA

    1,首先访问http://code.google.com/intl/zh-CN/apis/recaptcha/intro.html,然后点击左侧相应语言,博主示例用的jsp,所以下载jar包,可直接【点此下载jar包】解压zip中的recaptcha4j-0.0.7.jar至web项目lib下。

    2,访问https://www.google.com/recaptcha/admin/create并用google账户登录,在文本框输入自己网站的网址,如global-key.mycompany.com ,点击create key,生成Public Key和Private Key。

    3,在jsp中编写示例form

     <%@ page import="net.tanesha.recaptcha.ReCaptcha" %>
    <%@ page import="net.tanesha.recaptcha.ReCaptchaFactory" %>

    <html>
    <body>
    <form action="" method="post">
    <%
    ReCaptcha c
    = ReCaptchaFactory.newReCaptcha("填入之前生成的public_key", "填入之前生成的private_key", false);
    out.print(c.createRecaptchaHtml(
    null, null));
    %>
    <input type="submit" value="submit" />
    </form>
    </body>
    </html>

    4,编写示例服务端接收校验

    <%@ page import="net.tanesha.recaptcha.ReCaptchaImpl" %>
    <%@ page import="net.tanesha.recaptcha.ReCaptchaResponse" %>

    <html>
    <body>
    <%
    String remoteAddr = request.getRemoteAddr();
    ReCaptchaImpl reCaptcha
    = new ReCaptchaImpl();
    reCaptcha.setPrivateKey(
    "填入之前生成的private_key");

    String challenge = request.getParameter("recaptcha_challenge_field");
    String uresponse = request.getParameter("recaptcha_response_field");
    ReCaptchaResponse reCaptchaResponse
    = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);

    if (reCaptchaResponse.isValid()) {
    out.print(
    "Answer was entered correctly!");
    }
    else {
    out.print(
    "Answer is wrong");
    }
    %>
    </body>
    </html>




    Author:Pale Life
    From: 
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    linux 运维
    mariadb replication
    phpmyadmin
    Objective-C设计模式——单例Singleton(对象创建)
    收藏iOS学习资料
    axios拦截器
    vue单页面优化
    html设置http缓存代码
    js数组去重,排序的几种方法
    前端移动端问题
  • 原文地址:https://www.cnblogs.com/live365wang/p/2321548.html
Copyright © 2011-2022 走看看