简体   繁体   中英

recaptcha integration with Spring MVC

I am trying to integrate recaptcha into my spring mvc application. Below is the code.

@RequestMapping(value="home.htm", method=RequestMethod.POST)
public String processEmailLogin(
        @ModelAttribute("examLoginForm") ExamLoginDetails examLoginDetails,         
        BindingResult result, 
        Model model,
        @RequestParam("recaptcha_challenge_field") String challangeField, 
        @RequestParam("recaptcha_response_field") String responseField, 
        HttpServletRequest request, SessionStatus sessionStatus) {

    ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
    reCaptcha.setPrivateKey("6LcWOOsSAAAAAM48WFr4PfE0Y1LdTJHKC_BxILwl");

    /*String challenge = request.getParameter("recaptcha_challenge_field");
    String response = request.getParameter("recaptcha_response_field");*/

    String remoteAddr = request.getRemoteAddr();
    logger.info("Remote machine IP address is : "+remoteAddr);

    String emailIdForm = request.getParameter("email"); 
    ReCaptchaResponse reCaptchaResponse = reCaptchaService.checkAnswer(remoteAddr, challangeField, responseField);

    logger.info("Shown captcha is : "+challangeField);
    logger.info("Entered captcha is : "+responseField);
    logger.info("The validated recaptcha response is : "+reCaptchaResponse.isValid());

    boolean correctAnswer = false;
    correctAnswer = challangeField.equalsIgnoreCase(responseField);

    logger.info("Correct answer : "+correctAnswer);
    logger.info(":::::: LOADED Login Form CONTROLLER ::::::");

    if(!reCaptchaResponse.isValid()) {
        model.addAttribute("message", "wrong captcha");
        logger.info("Incorrect captcha");
        return "login/login";
    } 
    else {
        logger.info("::::: Checking captcha response to validate :::::");
        model.addAttribute("message", "correct captcha");
        LoginService loginService = (LoginService) ctx.getBean("loginService");
        examLoginDetails = loginService.performLogin(emailIdForm);
        logger.info("Email id is ::::::::::: "+examLoginDetails.getEmailId());
        return "home/home";
    }
}

My JSP form is as follows:

    <%@ page import="net.tanesha.recaptcha.ReCaptcha" %>  
<%@ page import="net.tanesha.recaptcha.ReCaptchaFactory" %>  
<!DOCTYPE html>  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>Insert title here</title>  
<script type="text/javascript" src="./jsp/scripts/jquery-1.9.1.js"></script>  
<script type="text/javascript" src="./jsp/scripts/jquery-ui-1.10.3.custom.min.js"></script>  
<link rel="stylesheet" type="text/css" href="./jsp/styles/jquery-ui-1.10.3.custom.min.css" />  
<link rel="stylesheet" type="text/css" href="./jsp/styles/page-style.css" />  
<link rel="stylesheet" type="text/css" href="./jsp/styles/tooltip.css" />  
</head>  
<body>  
<form action="home.htm" method="post" name="examLoginForm">  
<fieldset class="ui-widget ui-widget-content ui-corner-all">  
<legend class="ui-widget-header ui-corner-all">Online Examination</legend>  
<table  class="ui-widget ui-helper-clearfix">  
    <tr>  
        <td><label for="email">Please enter your Email Id : </label></td>  
        <td><input class="ui-widget-content ui-corner-all" type="text" id="emailId" name="email" title="Please provide the email id you used to register with us to take the exam." ></td>  
    </tr>  

                <tr>  
                    <td></td>  
                    <td>  

                      <script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=PUBLIC_KEY"></script>  
                    <noscript>  
                    <iframe src="http://www.google.com/recaptcha/api/noscript?k=PUBLIC_KEY" 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>  
                        <%-- <%  
                            ReCaptcha c = ReCaptchaFactory.newReCaptcha("PUBLIC_KEY","PRIVATE_KEY", false);   
                            out.print(c.createRecaptchaHtml(null, null));  
                         %> --%>  
                    </td>  
                    <td></td>  
                </tr>  

    <tr>  
        <td></td>  
        <td><span id="LoginBtn"><input class="ui-helper-clearfix ui-corner-all ui-button ui-button-text ui-state-default ui-state-focus ui-state-active ui-state-hover" type="submit" id="login" value="Take Exam" /></span></td>  
        <td></td>  
    </tr>  
 </table>  
</fieldset>  
</form>  
<div id="dialog" title="Confirm Your Email ID">  
 <p><span class="ui-widget ui-widget-content ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Are you sure that this is the Email Id you have used to register yourself with us?</p>  
</div>  

</body>  
</html>

The bean configuration in the spring application context is :

<bean id="reCaptchaService" class="net.tanesha.recaptcha.ReCaptchaImpl">  
    <property name="privateKey" value="PRIVATE_KEY" />  
</bean>

The output i see on my console is :

    13:12:56,875  INFO LoginFormController:58 - Remote machine IP address is : 10.129.75.57  
13:12:57,078  INFO LoginFormController:63 - Shown captcha is : 03AHJ_VuuVrDXTjd0tBEkuONNedE6Bk214GxuOj7DT0o927e5HNgS_IKX7Efuc79liUvsH8VbKbZ7aZ8iElpJog6YqjsvThFu3BuULzPGEPHZKiIk_hnix6N_IXC3aDQaEcBDTPgooOuqs_CwriWe8PuxrzfbzDS2QdnhLuiWqIVqlX2KnZT9BZYo  
13:12:57,078  INFO LoginFormController:64 - Entered captcha is : 44642526 2154  
13:12:57,079  INFO LoginFormController:65 - The validated recaptcha response is : false  
13:12:57,079  INFO LoginFormController:70 - Correct answer : false  
13:12:57,079  INFO LoginFormController:71 - :::::: LOADED Login Form CONTROLLER ::::::  
13:12:57,079  INFO LoginFormController:79 - ::::: Checking captcha response to validate :::::

What i see from the console output is that the recaptcha_challange_field contains the captcha question in some encripted form and the recaptcha_response_field contains the response for the captcha question in plain text form. So, is it the reason that the recaptcha checkAnswer(remoteAddr, challange, response) method is always evaluating to false?. Please help me as i am stuck with this problem from a long time now.

For me recaptcha-spring-boot-starter was very helpful and reduces your code a lot:

https://github.com/mkopylec/recaptcha-spring-boot-starter-samples

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM