【原创随笔】强大的谷歌开源免费验证码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>




posted @ 2012-01-13 14:02  为牛马者,背负前行  阅读(7561)  评论(0编辑  收藏  举报

如果本页面列出的内容侵犯了您的权益,请告知。
知识共享许可协议
996.icu