开发者

reCaptcha issue with Spring MVC

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.


What is the implementation of:

String html = reCaptcha.createRecaptchaHtml(null, null); ?

The reCaptcha html must have the name attribute as "recaptcha_challenge_field"

...

<textarea name="recaptcha_challenge_field" ... />
<input type="hidden" name="recaptcha_response_field" value="manual_challenge" />

...


Captcha is dynamic loaded script on the page. It is better to read captcha parameters from request object as shown in below example:

@RequestMapping(value="/submitCaptcha.web",method = RequestMethod.POST)
public String submitCaptcha(@ModelAttribute("recaptchaBean") RecaptchaBean recaptchaBean,BindingResult result, ModelMap model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    String captchaChallenge = request.getParameter("recaptcha_challenge_field");
    String captchaText = request.getParameter("recaptcha_response_field");  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜