reCaptcha issue with Spring MVC

Posted by skip on Stack Overflow See other posts from Stack Overflow or by skip
Published on 2011-02-14T10:58:24Z Indexed on 2011/02/14 15:25 UTC
Read the original article Hit count: 383

Filed under:
|

Hi

I've been trying to integrate reCaptcha with my application built on Spring framework, but I am getting this error:

org.springframework.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 recaptcha_challenge_field and recaptcha_response_field 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 http status 400 with the error org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'recaptcha_challenge_field' is not present:

UserManagementController.java

@Controller
public class UserManagementController {
    private static final String RECAPTCHA_HTML = "reCaptchaHtml";

    @Autowired
    private UserService userService;

    @Autowired
    private ReCaptcha reCaptcha;

    @RequestMapping(method=RequestMethod.GET, value="/addNewUser.do")
    public ModelAndView addNewUser() {
        User user = new User();
        String html = reCaptcha.createRecaptchaHtml(null, null);

        ModelMap modelMap = new ModelMap();
        modelMap.put("user", user);
        modelMap.put(RECAPTCHA_HTML, html);

        return new ModelAndView("/addNewUser", modelMap);
    }

    @RequestMapping(method=RequestMethod.POST, value="/addNewUser.do")
    public String addNewUser(@Valid  User user, BindingResult result,                                               
                                                @RequestParam("recaptcha_challenge_field") String challenge,
                                                @RequestParam("recaptcha_response_field") String response,
                                                HttpServletRequest request,                                             
                                                Model model) {

        verifyBinding(result);
        String remoteAddr = request.getRemoteAddr();
        ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, response);
        if (!reCaptchaResponse.isValid()) {
            result.rejectValue("captcha", "errors.badCaptcha");
            }

        model.addAttribute("user", user);
        if (result.hasErrors()) {
            result.reject("form.problems");
            return "addNewUser";
        }
        return "redirect:showContent.do";
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setAllowedFields(new String[] { 
            "firstName", "lastName", "email",
            "username", "password", "recaptcha_challenge_field", "recaptcha_response_field"
        });
    }

    private void verifyBinding(BindingResult result) {
        String[] suppressedFields = result.getSuppressedFields();
        if (suppressedFields.length > 0) {
            throw new RuntimeException("You've attempted to bind fields that haven't been allowed in initBinder(): " 
                    + StringUtils.join(suppressedFields, ", "));
        }
    }
}

Here is the addNewUser.jsp element on the form page for the above controller:

        <tr>
            <td>Please prove you're a person</td>
            <td>${reCaptchaHtml}</td>
            <td><form:errors path="captcha" cssStyle="color:red"></form:errors></td>
        </tr>

Could you help me understand what am I missing here? Thanks for reply.

© Stack Overflow or respective owner

Related posts about spring-mvc

Related posts about recaptcha