【原創隨筆】強大的谷歌開源免費驗證碼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>





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM