Back Forum Reply New

reCaptcha issue with Spring MVC

Hi

I've been trying to integrate reCaptcha with my application built on Spring framework, but I am getting this error:Code:
org..web.bind.MissingServletRequestParameterException: Required String parameter 'recaptcha_challenge_field' is not present
Could someone help me understand that why am I getting this error. I've got both  quot;recaptcha_challenge_fieldquot; and quot;recaptcha_response_fieldquot; parameters bound to the User domain object.

Could anybody help me understand what am I missing?

Thanks

Here is the code of the controller I am using, all I am trying to do is register a user with reCaptcha functionality but what I am getting is a from status 400 with the error
Code:
org..web.bind.MissingServletRequestParameterException: Required String parameter 'recaptcha_challenge_field' is not present
:

UserManagementController.javaCode:   @Controller   public class UserManagementController {   private static final String RECAPTCHA_HTML = quot;reCaptchaHtmlquot;;      @Autowired   private UserService userService;      @Autowired   private ReCaptcha reCaptcha;      @RequestMapping(method=RequestMethod.GET, value=quot;/addNewUser.doquot;)   public ModelAndView addNewUser() {   User user = new User();   String html = reCaptcha.createRecaptchaHtml(null, null);      ModelMap modelMap = new ModelMap();   modelMap.put(quot;userquot;, user);   modelMap.put(RECAPTCHA_HTML, html);      return new ModelAndView(quot;/addNewUserquot;, modelMap);   }      @RequestMapping(method=RequestMethod.POST, value=quot;/addNewUser.doquot;)   public String addNewUser(@Valid  User user, BindingResult result,    @RequestParam(quot;recaptcha_challenge_fieldquot;) String challenge,   @RequestParam(quot;recaptcha_response_fieldquot;) String response,   fromServletRequest request,   Model model) {      verifyBinding(result);   String remoteAddr = request.getRemoteAddr();       ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, response);   if (!reCaptchaResponse.isValid()) {   result.rejectValue(quot;captchaquot;, quot;errors.badCaptchaquot;);   }      model.addAttribute(quot;userquot;, user);   if (result.hasErrors()) {   result.reject(quot;form.problemsquot;);   return quot;addNewUserquot;;   }   return quot;redirect:showContent.doquot;;   }      @InitBinder   public void initBinder(WebDataBinder binder) {   binder.setAllowedFields(new String[] {    quot;firstNamequot;, quot;lastNamequot;, quot;emailquot;,   quot;usernamequot;, quot;passwordquot;, quot;recaptcha_challenge_fieldquot;, quot;recaptcha_response_fieldquot;   });   }      private void verifyBinding(BindingResult result) {   String[] suppressedFields = result.getSuppressedFields();   if (suppressedFields.length gt; 0) {   throw new RuntimeException(quot;You've attempted to bind fields that haven't been allowed in initBinder(): quot;    + StringUtils.join(suppressedFields, quot;, quot;));   }   }   }
Here is the addNewUser.jsp element on the form page for the above controller:Code:
lt;trgt;
lt;tdgtlease prove you're a personlt;/tdgt;
lt;tdgt;${reCaptchaHtml}lt;/tdgt;
lt;tdgt;lt;form:errors path=quot;captchaquot; cssStyle=quot;color:redquot;gt;lt;/form:errorsgt;lt;/tdgt;
lt;/trgt;
Could someone help me with it?

Thanks.

This won't work. Spring Web MVC has its own internal mechanism to handle low-level web objects like session, request and their interactions, hiding them from the developer which works with an higher level of abstraction (annotations and form: elements). This works fine provided you never quot;breakquot; the mechanism by trying to handle low level objects yourself or letting other frameworks do it. In your case, you are generating html fragments with recaptcha and passing them directly to the page; these fragments handle the request (and maybe also the session) in their own peculiar way and this most probably breaks the integrity of the Spring web mvc flow. I'd do a quick search to see if there is an already implemented support / integration between Spring Web MVC and ReCaptcha; if yes, use it; if not, then it's best if you abandon recaptcha in favor of a solution that can integrate well with Spring.

Hi Enrico, thanks for reply. Yeah I removed the
Code:
@RequestParam(quot;recaptcha_challenge_fieldquot;) String challengeand
Code:
@RequestParam(quot;recaptcha_response_fieldquot;) String responseand tried
Code:
String challenge = (String) request.getAttribute(quot;recaptcha_challenge_fieldquot;)and
Code:
String response = (String) request.getAttribute(quot;recaptcha_response_fieldquot;)
, what I found was that I was getting null value for both recaptcha_response_field and recaptcha_response_field.
I'll try some other captcha to get integrated with Spring MVC. It would be helpful if you cud tell me about the one that you think I should use.

Thanks for reply

I'm by no means an expert when it comes to captcha but googling around I found that JCaptcha seems to integrate easily with Spring...maybe that could be worth exploring.

Hi Enrico, I am trying to integrate jCaptcha only. Thanks

Hi Enrico, I've got jCaptcha working, but I m still missing reCaptcha as I had it working with spring 2.5 sometime ago, wonder what am I missing this time with Spring 3.

Thanks

@skipskipping, I'm not sure if this would help you. I've prepared a guide for integrating reCAPTCHA with Spring Security and Spring MVC.
Spring Security 3: Integrating reCAPTCHA Service at 2011/02...recaptcha.html

It might be an overkill since your use case requires only reCAPTCHA and Spring MVC. It shouldn't be that difficult though if you're just using MVC
¥
Back Forum Reply New