Package com.sahuaro.captcha

Source Code of com.sahuaro.captcha.ReCaptchaUtils

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();
    }

}
TOP

Related Classes of com.sahuaro.captcha.ReCaptchaUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.