Spring MVC 的 reCaptcha 问题
我一直在尝试将 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:必需的字符串参数“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实现是什么:
reCaptcha html 必须将 name 属性设置为“recaptcha_challenge_field
……
”
What is the implementation of:
The reCaptcha html must have the name attribute as "recaptcha_challenge_field"
...
...
验证码是页面上动态加载的脚本。最好从请求对象中读取验证码参数,如下例所示:
Captcha is dynamic loaded script on the page. It is better to read captcha parameters from request object as shown in below example: