Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 使用Spring MVC重新分析TCHA问题_Spring Mvc_Recaptcha - Fatal编程技术网

Spring mvc 使用Spring MVC重新分析TCHA问题

Spring mvc 使用Spring MVC重新分析TCHA问题,spring-mvc,recaptcha,Spring Mvc,Recaptcha,我一直在尝试将reCaptcha与基于Spring framework构建的应用程序集成,但我遇到了以下错误: org.springframework.web.bind.MissingServletRequestParameterException:所需字符串参数“recaptcha\u challenge\u field”不存在 有人能帮我理解为什么我会犯这个错误吗。我已将recaptcha\u challenge\u字段和recaptcha\u response\u字段参数绑定到用户域对象

我一直在尝试将reCaptcha与基于Spring framework构建的应用程序集成,但我遇到了以下错误:

org.springframework.web.bind.MissingServletRequestParameterException
:所需字符串参数“recaptcha\u challenge\u field”不存在

有人能帮我理解为什么我会犯这个错误吗。我已将
recaptcha\u challenge\u字段
recaptcha\u response\u字段
参数绑定到
用户
域对象

谁能帮我理解我错过了什么

谢谢

这是我正在使用的控制器的代码,我只是想用reCaptcha功能注册一个用户,但我得到的是一个
http状态400
,错误是
org.springframework.web.bind.MissingServletRequestParameterException:必需的字符串参数“reCaptcha\u challenge\u字段”不存在

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>

请证明你是一个人
${reCaptchaHtml}
你能帮我理解我在这里遗漏了什么吗?
感谢您的回复。

以下措施的实施情况:

String html = reCaptcha.createRecaptchaHtml(null, null); ?
reCaptcha html必须具有名称属性“reCaptcha\u挑战\u字段”



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

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

请发布一些与此问题相关的代码,例如控制器代码。@skip:看不出您的代码有任何错误,几乎相同的代码对我来说很好。@axtavt:我删除了
@RequestParam(“recaptcha\u challenge\u field”)字符串challenge
@RequestParam(“recaptcha\u response\u field”)String response
并尝试了
String challenge=(String)request.getAttribute(“recaptcha\u challenge\u字段”)
String response=(String)request.getAttribute(“recaptcha\u response\u字段”)
,我发现我得到的是recaptcha\u response\u字段和recaptcha\u response\u字段的空值。知道我该怎么做吗?@skip:在构造
ReCaptcha
时,尝试将
includeNoScript
设置为
true
,并查看这些字段是否包含在呈现的HTML中。@axtavt:是的,它正在创建这些字段,但这两个字段的值仍然为空:(
@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");  }