1 用户访问网站的页面,同时会出发页面中的JS去访问recaptcha的认证服务
2 recaptcha直接返回需要识别的图片和这个图片对应的一段加密的数据
3 用户输入认证码后,程序提交用户的输入和之前recaptcha返回的加密数据段到我们网站的后台
4 后台程序根据用户输入、加密的数据、访问用户的IP地址和在recaptcha注册时得到的private key。发送认证请求到recaptcha。
5 通过返回的信息,确认用户的输入是否正确。
如果想用recaptcha的服务只需要简单的几个步骤
1 访问recaptcha.net,注册新用户,确定需要认证服务的网站域名(www.ct.com)。得到相应的公钥和私钥
2 创建测试的页面c.html放到www.ct.com的根目录,代码如下
<html>
<body>
<form action="/test-recaptcha.jsp" method="post">
<script type="text/javascript"
src="http://api.recaptcha.net/challenge?k=6Lfp9QMAAAAAADSCcn_ZEuo1LtxReBaT2t1kVnL1">
</script>
<noscript>
<iframe src="http://api.recaptcha.net/noscript?k=6Lfp9QMAAAAAADSCcn_ZEuo1LtxReBaT2t1kVnL1"
height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge">
</noscript>
<input type="submit" name="test" value="test" />
</form>
</body>
</html>
3 访问www.ct.com/c.html后看到如下页面
4 创建test-recaptcha.jsp来判断用户的输入是否OK
<%
ReCaptchaImpl cha = new ReCaptchaImpl();
cha.setPrivateKey("私钥");
ReCaptchaResponse res = cha.checkAnswer(request.getRemoteAddr(), request.getParameter("recaptcha_challenge_field"), request.getParameter("recaptcha_response_field"));
if(res.isValid()) {
//校验失败
out.println(res.getErrorMessage());
}
else {
//成功
}
%>