我一直在尝试将reCaptcha与我在Spring框架上构建的应用程序集成,但是我收到了这个错误:
org.springframework.web.bind.MissingServletRequestParameterException
:必需的字符串参数'recaptcha_challenge_field'不存在
有人可以帮我理解为什么我会收到此错误。我将recaptcha_challenge_field
和recaptcha_response_field
个参数绑定到User
域对象。
有人能帮我理解我错过了什么吗?
由于
以下是我正在使用的控制器的代码,我要做的就是使用reCaptcha功能注册用户,但我得到的是http status 400
,错误为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, ", "));
}
}
}
以下是上述控制器的表单页面上的 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>
你能帮我理解我在这里缺少什么吗? 谢谢你的回复。
答案 0 :(得分:1)
实施的是什么:
String html = reCaptcha.createRecaptchaHtml(null, null); ?
reCaptcha html的名称属性必须为“recaptcha_challenge_field”
...
<textarea name="recaptcha_challenge_field" ... />
<input type="hidden" name="recaptcha_response_field" value="manual_challenge" />
...
答案 1 :(得分:0)
Captcha是页面上的动态加载脚本。最好从请求对象中读取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"); }