【发布时间】:2018-03-21 08:15:24
【问题描述】:
我正在为网站构建一个功能,用户可以在其中重置密码。他在 url 中收到一封带有生成令牌的电子邮件。单击此链接时,用户将被发送到 /reset 页面。该页面的 Get 方法如下:
@RequestMapping(value = "/reset", method = RequestMethod.GET)
public ModelAndView displayResetPasswordPage(ModelAndView modelAndView, @ModelAttribute User user, @RequestParam("token") String token) {
User u = userService.findByResetToken(token);
if (u == null) {
modelAndView.setViewName("error/404");
return modelAndView;
} else {
modelAndView.addObject("token", token);
modelAndView.setViewName("resetPassword");
return modelAndView;
}
}
这很好用,如果 url 中的令牌发生更改,用户将被发送到错误页面。现在我想将这个“令牌”参数传递给 post 方法:
@RequestMapping(value = "/reset", method = RequestMethod.POST)
public ModelAndView setNewPassword(ModelAndView modelAndView, @RequestParam Map<String, String> requestParams, @ModelAttribute User user, BindingResult bindingResult, RedirectAttributes redir) {
System.out.println("token: " + requestParams.get("token"));
User u = userService.findByResetToken(requestParams.get("token"));
// User u = userService.findByEmail(user.getEmail());
if (u != null) {
User updatedUser = userService.updateUserPassword(user);
modelAndView.addObject("User",updatedUser);
modelAndView.setViewName("redirect:login");
return modelAndView;
} else {
modelAndView.setViewName("resetPassword");
}
return modelAndView;
}
这总是返回 resetPassword 视图,因为 requestparams.get("token") 总是返回一个空字符串。我没有使用正确的方法来获取参数值吗?
重置密码视图:
<div class="wrapper">
<form class="form-signin" th:action="@{/reset}" method="post" th:object="${user}">
<h2 class="form-signin-heading">Reset wachtwoord</h2>
<input type="hidden" name="token" th:value="*{resetToken}"/>
<div class="form-group">
<input type="password" th:field="*{encryptedPassword}" id="password" class="form-control input-lg"
placeholder="Password" tabindex="3"/>
<div class="form-group">
<input type="password" th:field="*{matchingPassword}" id="password_confirmation"
class="form-control input-lg" placeholder="Confirm Password" tabindex="4"/>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<input type="submit" class="btn btn-secondary" value="Registreer"/>
</div>
</div>
</form>
【问题讨论】:
-
“/reset”视图是什么样的?
-
@LeoG。编辑了我原来的帖子
-
不应该是“token”而不是“resetToken”,正如您在控制器中命名的那样?
-
我重命名了modelAndView.addObject("token", token);到 modelAndView.addObject("resetToken", token);但这也不起作用
-
有一个自定义的 DTO 而不是用户,可能是一个包含 Userdetails 和令牌的 DTO
标签: spring spring-mvc spring-boot