title: recaptcha integration with Spring MVC
body: <p>I am trying to integrate recaptcha into my spring mvc application. Below is the code.</p>
  
  <pre><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";
      }
  }
  </code></pre>
  
  <p>My JSP form is as follows:</p>
  
  <pre><code>    &lt;%@ page import="net.tanesha.recaptcha.ReCaptcha" %&gt;  
  &lt;%@ page import="net.tanesha.recaptcha.ReCaptchaFactory" %&gt;  
  &lt;!DOCTYPE html&gt;  
  &lt;html&gt;  
  &lt;head&gt;  
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;  
  &lt;title&gt;Insert title here&lt;/title&gt;  
  &lt;script type="text/javascript" src="./jsp/scripts/jquery-1.9.1.js"&gt;&lt;/script&gt;  
  &lt;script type="text/javascript" src="./jsp/scripts/jquery-ui-1.10.3.custom.min.js"&gt;&lt;/script&gt;  
  &lt;link rel="stylesheet" type="text/css" href="./jsp/styles/jquery-ui-1.10.3.custom.min.css" /&gt;  
  &lt;link rel="stylesheet" type="text/css" href="./jsp/styles/page-style.css" /&gt;  
  &lt;link rel="stylesheet" type="text/css" href="./jsp/styles/tooltip.css" /&gt;  
  &lt;/head&gt;  
  &lt;body&gt;  
  &lt;form action="home.htm" method="post" name="examLoginForm"&gt;  
  &lt;fieldset class="ui-widget ui-widget-content ui-corner-all"&gt;  
  &lt;legend class="ui-widget-header ui-corner-all"&gt;Online Examination&lt;/legend&gt;  
  &lt;table  class="ui-widget ui-helper-clearfix"&gt;  
      &lt;tr&gt;  
          &lt;td&gt;&lt;label for="email"&gt;Please enter your Email Id : &lt;/label&gt;&lt;/td&gt;  
          &lt;td&gt;&lt;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." &gt;&lt;/td&gt;  
      &lt;/tr&gt;  
  
                  &lt;tr&gt;  
                      &lt;td&gt;&lt;/td&gt;  
                      &lt;td&gt;  
  
                        &lt;script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=PUBLIC_KEY"&gt;&lt;/script&gt;  
                      &lt;noscript&gt;  
                      &lt;iframe src="http://www.google.com/recaptcha/api/noscript?k=PUBLIC_KEY" height="300" width="500" frameborder="0"&gt;&lt;/iframe&gt;&lt;br&gt;  
                      &lt;textarea name="recaptcha_challenge_field" rows="3" cols="40"&gt;&lt;/textarea&gt;  
                      &lt;input type="hidden" name="recaptcha_response_field" value="manual_challenge"&gt;  
                      &lt;/noscript&gt;  
                          &lt;%-- &lt;%  
                              ReCaptcha c = ReCaptchaFactory.newReCaptcha("PUBLIC_KEY","PRIVATE_KEY", false);   
                              out.print(c.createRecaptchaHtml(null, null));  
                           %&gt; --%&gt;  
                      &lt;/td&gt;  
                      &lt;td&gt;&lt;/td&gt;  
                  &lt;/tr&gt;  
  
      &lt;tr&gt;  
          &lt;td&gt;&lt;/td&gt;  
          &lt;td&gt;&lt;span id="LoginBtn"&gt;&lt;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" /&gt;&lt;/span&gt;&lt;/td&gt;  
          &lt;td&gt;&lt;/td&gt;  
      &lt;/tr&gt;  
   &lt;/table&gt;  
  &lt;/fieldset&gt;  
  &lt;/form&gt;  
  &lt;div id="dialog" title="Confirm Your Email ID"&gt;  
   &lt;p&gt;&lt;span class="ui-widget ui-widget-content ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"&gt;&lt;/span&gt;Are you sure that this is the Email Id you have used to register yourself with us?&lt;/p&gt;  
  &lt;/div&gt;  
  
  &lt;/body&gt;  
  &lt;/html&gt;
  </code></pre>
  
  <p>The bean configuration in the spring application context is :</p>
  
  <pre><code>&lt;bean id="reCaptchaService" class="net.tanesha.recaptcha.ReCaptchaImpl"&gt;  
      &lt;property name="privateKey" value="PRIVATE_KEY" /&gt;  
  &lt;/bean&gt;
  </code></pre>
  
  <p>The output i see on my console is :</p>
  
  <pre><code>    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 :::::
  </code></pre>
  
  <p>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.</p>