Spring MVC 的 reCaptcha 问题

发布于 2024-10-17 02:13:15 字数 3433 浏览 0 评论 0原文

我一直在尝试将 reCaptcha 与基于 Spring 框架构建的应用程序集成,但出现以下错误:

org.springframework.web.bind.MissingServletRequestParameterException:必需的字符串参数“recaptcha_challenge_field”不存在

有人可以帮助我理解为什么我会收到此错误。我已将 recaptcha_challenge_fieldrecaptcha_response_field 参数绑定到 User 域对象。

有人可以帮助我理解我错过了什么吗?

谢谢

这是我正在使用的控制器的代码,我想做的就是使用 reCaptcha 功能注册用户,但我得到的是 http status 400 ,并带有错误 org. springframework.web.bind.MissingServletRequestParameterException:必需的字符串参数“recaptcha_challenge_field”不存在

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, ", "));
        }
    }
}

这是表单页面上的addNewUser.jsp元素对于上述控制器:

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

你能帮我理解我在这里缺少什么吗? 感谢您的回复。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃素的狼 2024-10-24 02:13:15

实现是什么:

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

reCaptcha html 必须将 name 属性设置为“recaptcha_challenge_field

……

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

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" />

...

烟若柳尘 2024-10-24 02:13:15

验证码是页面上动态加载的脚本。最好从请求对象中读取验证码参数,如下例所示:

@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");  }

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");  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文