Wil*_*ass 23 java spring spring-mvc
我看到一个Spring MVC控制器的奇怪问题.此方法用于设置密码.它需要两个表单参数"password"和"confirmPassword".第一次调用表单时,这很好 - 将字段传递给方法.
第二次提交表单时会出现此问题.如果表单第一次填写不正确,则会正确地将用户发送回表单页面并提示再次输入密码.但是,第二次尝试时该方法的参数不正确.参数是逗号分隔的列表,其中包括与第二个连接的第一个表单条目.
例:
带字段"password"的第一个表单帖子的值为"abc".方法参数"password"的值为"abc".
带有字段"password"和值"xyz"的第二个表单帖子.方法参数"password"的值为"xyz,abc".
Spring MVC文档并没有说明有用.不知何故,旧的表格帖子被记住并包括在内.有人有解决这个问题的经验吗?
控制器方法如下:
@RequestMapping(value = "/account/reset", method = RequestMethod.POST)
public String resetPassword(@RequestParam("password") String password,
@RequestParam("confirmPassword") String confirmPassword,
@RequestParam("hash") String hash, ModelMap model) throws EncryptionException
{
String userName = stringEncrypterService.decrypt(hash);
User user = userService.findUserByPath(userName);
if (!password.equals(confirmPassword))
{
model.put("hash", hash);
model.put("user", user);
model.put("error",
"The two passwords you entered below do not match. Please try again.");
return "users/resetPassword";
}
userService.updatePassword(user, password);
emailService.sendUserInfoChange(user);
return "redirect:/session/signin?passwordReset=true";
}
Run Code Online (Sandbox Code Playgroud)
更新.一些响应者已经建议可能有问题的帖子有额外的URL参数或隐藏的表单字段导致重复的字段名称.我向Fiddler确认情况并非如此.这是第三次尝试的原始请求.(略微编辑以删除会话cookie).
POST http://wintest.foriodev.com/simulate/account/reset/ HTTP/1.1
Host: wintest.foriodev.com
Connection: keep-alive
Referer: http://wintest.foriodev.com/simulate/account/reset/
Content-Length: 73
Cache-Control: max-age=0
Origin: http://wintest.foriodev.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16
Content-Type: application/x-www-form-urlencoded
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: AUTOLOGIN_TOKEN=xyz; SIMULATE_WORKER=wintest; JSESSIONID=xyz;
password=a&hash=xyz&confirmPassword=a&save=Reset+Password
Run Code Online (Sandbox Code Playgroud)
小智 8
我认为这是因为
return" redirect:/session/signin?passwordReset = true";
使用重定向:框架使用url重写技术,类似于servlet中的基本response.sendRedirect(...),因此参数值与请求一起附加到下一个后续请求,依此类推.
尝试使用不同的机制而不是" 重定向: "
所以经过一年多的努力,我发现了这一点.
问题是一个自定义拦截器,它在每个请求上存储了请求缓存.我这样做是为了当用户登录时,他将返回上一个屏幕.但这对于"重置密码"屏幕来说完全不合适.
机制是当调用request.getParameter(Name)时,SavedRequestCacheWrapper会将实际的HTTP请求参数与最后一个请求中存储的参数连接起来.
解决方案是(a)让这个拦截器忽略重置密码屏幕和(b)忽略所有发布请求以防止这种请求参数值连接.
对于其他响应者,感谢所有好的想法.(以及对此给予赏金的匿名用户 - 感谢您让我回去看看.)
| 归档时间: |
|
| 查看次数: |
9344 次 |
| 最近记录: |