实现功能
当用户在账号设置页面修改密码时,输入完原始密码以及新密码、确认密码后,当点击【立即保存】后,若用户原始密码输入正确,新密码与确认密码一致时,会跳转至登录页面重新登录,若输入有误,则显示对应的错误信息
controller层
//修改密码
@RequestMapping(value = "/updatePassword",method = RequestMethod.POST)//@CookieValue("ticket") String ticket:从浏览器器中得到cookie
public String updatePassword(String oldPassword, String newPassword, String confirmNewPassword, Model model,@CookieValue("ticket") String ticket){
//对传入参数进行判断,不能为空
if(StringUtils.isBlank(oldPassword)){
model.addAttribute("oldPasswordMsg","请输入原始密码!");
return "site/setting";
}
if(StringUtils.isBlank(newPassword)){
model.addAttribute("newPasswordMsg","请输入新密码!");
return "site/setting";
}
if(StringUtils.isBlank(confirmNewPassword)){
model.addAttribute("confirmNewPasswordMsg","请输入确认密码!");
return "site/setting";
}
//首先通过所持有的的用户对象获取当前用户
User user = hostHolder.getUser();
//判断用户输入的原密码是否与存储的原密码一致
//首先对用户输入的原密码进行加密处理
oldPassword = CommunityUtil.md5(oldPassword+user.getSalt());
if(!oldPassword.equals(user.getPassword())){
model.addAttribute("oldPasswordMsg","该密码与原密码不符!");
return "site/setting";
}
//判断新输入密码与原密码是否一致
//对新密码进行加密
newPassword=CommunityUtil.md5(newPassword+user.getSalt());
if(newPassword.equals(user.getPassword())){//判断
model.addAttribute("newPasswordMsg","新密码与原密码一致!");
return "site/setting";
}
//对确认密码进行加密
confirmNewPassword=CommunityUtil.md5(confirmNewPassword+user.getSalt());
if(!newPassword.equals(confirmNewPassword)){//判断
model.addAttribute("confirmNewPasswordMsg","两次密码不一致!");
return "site/setting";
}
userService.updatePassword(user.getId(),newPassword);
//修改密码后,用户需要重新登陆,所以在本次持有中释放用户
userService.logout(ticket);
return "redirect:/login";
}
service层
通过当前的用户id,和传入的密码,修改用户的密码
//修改密码
public int updatePassword(int userId,String password){
return userMapper.updatePassword(userId,password);
}
setting.html
<!-- 修改密码 -->
<h6 class="text-left text-info border-bottom pb-2 mt-5">修改密码</h6>
<form class="mt-5" method="post" th:action="@{/user/updatePassword}">
<div class="form-group row mt-4">
<label for="old-password" class="col-sm-2 col-form-label text-right">原密码:</label>
<div class="col-sm-10">
<input type="password" th:class="|form-control ${oldPasswordMsg!=null ? 'is-invalid':'' }|"
name="oldPassword"
id="old-password" placeholder="请输入原始密码!" required>
<div class="invalid-feedback" th:text="${oldPasswordMsg}">
密码长度不能小于8位!
</div>
</div>
</div>
<div class="form-group row mt-4">
<label for="new-password" class="col-sm-2 col-form-label text-right">新密码:</label>
<div class="col-sm-10">
<input type="password" th:class="|form-control ${newPasswordMsg!=null ? 'is-invalid':'' }|"
name="newPassword"
id="new-password" placeholder="请输入新的密码!" required>
<div class="invalid-feedback" th:text="${newPasswordMsg}">
密码长度不能小于8位!
</div>
</div>
</div>
<div class="form-group row mt-4">
<label for="confirm-password" class="col-sm-2 col-form-label text-right">确认密码:</label>
<div class="col-sm-10">
<input type="password" th:class="|form-control ${confirmNewPasswordMsg!=null ? 'is-invalid':'' }|"
name="confirmNewPassword"
id="confirm-password" placeholder="再次输入新密码!" required>
<div class="invalid-feedback" th:text="${confirmNewPasswordMsg}">
两次输入的密码不一致!
</div>
</div>
</div>
<div class="form-group row mt-4">
<div class="col-sm-2"></div>
<div class="col-sm-10 text-center">
<button type="submit" class="btn btn-info text-white form-control">立即保存</button>
</div>
</div>
</form>