reCaptcha验证码 ( by quqi99 )
作者:张华 发表于:2011-01-11
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明
1) util类:
+package com.daodao.util;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLEncoder;
+
+import com.TripResearch.util.Logging;
+
+public class GoogleCaptchaUtil
+{
+ private static final String PUBLIC_KEY = "到goole申请";
+ private static final String PRIVATE_KEY = "到goole申请";
+ private static final String VERIFY_URL = "http://api-verify.recaptcha.net/verify";
+ private static final String URL_CHARACTER_ENCODING = "UTF-8";
+
+ public static String getPublicKey()
+ {
+ return PUBLIC_KEY;
+ }
+
+ public static boolean checkAnswer(String remoteAddr, String challenge, String response)
+ {
+ String postParameters;
+ if(remoteAddr == null || challenge == null || response == null)
+ {
+ return false;
+ }
+ try
+ {
+ postParameters = "privatekey=" + URLEncoder.encode(PRIVATE_KEY,URL_CHARACTER_ENCODING) + "&remoteip=" + URLEncoder.encode(remoteAddr,URL_CHARACTER_ENCODING) +
+ "&challenge=" + URLEncoder.encode(challenge,URL_CHARACTER_ENCODING) + "&response=" + URLEncoder.encode(response,URL_CHARACTER_ENCODING);
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ Logging.SERVLET.debug("Unsupported Encoding: " + e.getMessage());
+ return false;
+ }
+ String message = httpPost(VERIFY_URL, postParameters);
+
+ if (message == null) {
+ Logging.SERVLET.debug("Null read from server.");
+ return false;
+ }
+
+ String[] a = message.split("/r?/n");
+ if (a.length < 1) {
+ Logging.SERVLET.debug("No answer returned from recaptcha: " + message);
+ return false;
+ }
+ return "true".equals(a[0]);
+ }
+
+ public static String httpPost(String urlS, String postdata)
+ {
+ InputStream in = null;
+ URLConnection connection = null;
+ try
+ {
+ URL url = new URL(urlS);
+ connection = url.openConnection();
+
+ connection.setDoOutput(true);
+ connection.setDoInput(true);
+ connection.setConnectTimeout(10000);
+ connection.setReadTimeout(10000);
+
+ OutputStream out = connection.getOutputStream();
+ out.write(postdata.getBytes());
+ out.flush();
+
+ in = connection.getInputStream();
+
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
+ byte[] buf = new byte[1024];
+ while (true)
+ {
+ int rc = in.read(buf);
+ if (rc <= 0)
+ break;
+ else
+ bout.write(buf, 0, rc);
+ }
+
+ out.close();
+ in.close();
+
+ return bout.toString();
+ }
+ catch (IOException e)
+ {
+ Logging.SERVLET.debug("Cannot load URL: " + e.getMessage());
+ return null;
+ }
+ finally
+ {
+ try
+ {
+ if (in != null)
+ in.close();
+ }
+ catch (Exception e)
+ {
+ // swallow.
+ }
+ }
+ }
+
+}
2)这样调用util类:
+ public boolean verifyGoogleCaptcha(CustomServletRequest request)
+ {
+ String remoteAddr = request.getRemoteAddr();
+ String challenge = request.getParameter("recaptcha_challenge_field");
+ String uresponse = request.getParameter("recaptcha_response_field");
+ return GoogleCaptchaUtil.checkAnswer(remoteAddr, challenge, uresponse);
+ }
3)添加vm宏:
+#macro( PrepareGoogleCaptcha )
+ <script type="text/javascript" >
+ var RecaptchaOptions = { theme : 'custom', custom_theme_widget: 'recaptcha_widget' };
+ </script>
+#end
+#macro( ShowGoogleCaptcha )
+ <style type="text/css">
+ #recaptcha_widget { margin-bottom:10px; }
+ #recaptcha_image, #recaptcha_image img { width:180px !important; height:40px !important; }
+ .change-image { float:left; margin:14px 0 0; }
+ .mt5 { margin-top:5px; }
+ </style>
+ <div id="recaptcha_widget" style="display:none">
+ <div class="clearfix">
+ <div id="recaptcha_image" class="left"></div>
+ <div class="change-image"><a href="javascript:Recaptcha.reload()">看不清? 换一张!</a></div>
+ </div>
+ <div class="mt5">
+ <label for="recaptcha_response_field">请输入验证码:</label>
+ <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
+ </div>
+ </div>
+ <div id="CAPTCHA">
+ <script type="text/javascript"
+ src="http://www.google.com/recaptcha/api/challenge?k=$recaptchaPubkey">
+ </script>
+ <noscript>
+ <iframe src="http://www.google.com/recaptcha/api/noscript?k=$recaptchaPubkey" 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>
+ </div>
+#end
3) 在vm中用上面的宏
#PrepareGoogleCaptcha()
+ <div class="error">
+ <b>$util.localize("rd_forums_error_header_ffffee49")</b>
+ <ul> <li>- 检证码错误,请重新输入。 </li> </ul>
+ </div>
#ShowGoogleCaptcha()
5) servlet中:
if(ServletUtils.getInstance().verifyGoogleCaptcha(request)){
addObject("invalidCaptcha", true);
// do other
}