package com.sahuaro.captcha;
import javax.servlet.http.HttpServletRequest;
import net.tanesha.recaptcha.ReCaptcha;
import net.tanesha.recaptcha.ReCaptchaFactory;
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;
public class ReCaptchaUtils {
private static final String PUBLIC_KEY = "6Lerle0SAAAAAGNEDbH42bp3GFxyFXUVo9Hntkk7";
private static final String PRIVATE_KEY = "6Lerle0SAAAAAF4lafLy5LywL3k850X1IJ3al5uO";
private ReCaptchaUtils() {}
public static String createRecaptchaHtml() {
ReCaptcha c = ReCaptchaFactory.newReCaptcha(PUBLIC_KEY, PRIVATE_KEY, true);
return c.createRecaptchaHtml(null, null);
}
public static boolean checkRecaptchaResponse(HttpServletRequest request) {
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);
return reCaptchaResponse.isValid();
}
}