-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f6a3815
commit c6b482f
Showing
9 changed files
with
199 additions
and
26 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
133 changes: 133 additions & 0 deletions
133
help/2-LoginAndRegister/7-AccountSetting/7.2-ForgetPassword.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# 忘记密码 | ||
|
||
1. UserService: | ||
|
||
```java | ||
// 重置密码 - forget | ||
public Map<String, Object> resetPassword(String email, String password) { | ||
Map<String, Object> map = new HashMap<>(); | ||
if (StringUtils.isBlank(email)) { | ||
map.put("emailMsg", "邮箱不能为空!"); | ||
return map; | ||
} | ||
if (StringUtils.isBlank(password)) { | ||
map.put("passwordMsg", "密码不能为空!"); | ||
return map; | ||
} | ||
if(password.length()<8){ | ||
map.put("passwordMsg", "密码不能少于8位!"); | ||
return map; | ||
} | ||
User user = userMapper.selectByEmail(email); | ||
if (user == null) { | ||
map.put("emailMsg", "该邮箱不存在!"); | ||
return map; | ||
} | ||
password = CommunityUtil.md5(password + user.getSalt()); | ||
if (user.getPassword().equals(password)) { | ||
map.put("passwordMsg", "新密码不能和原密码相同!"); | ||
return map; | ||
} | ||
userMapper.updatePassword(user.getId(), password); | ||
clearCache(user.getId()); | ||
return map; | ||
} | ||
``` | ||
|
||
2. LoginController: | ||
|
||
```java | ||
@RequestMapping(path = "/forget" ,method = RequestMethod.GET) | ||
public String getForgetPage(){ | ||
return "/site/forget"; | ||
} | ||
|
||
@RequestMapping(path = "/forgetPassword", method = RequestMethod.POST) | ||
public String updatePassword(String email, String password ,Model model){ | ||
Map<String, Object> map = userService.resetPassword(email, password); | ||
if (map == null || map.isEmpty()) { | ||
return "redirect:/logout"; | ||
} else { | ||
model.addAttribute("emailMsg", map.get("emailMsg")); | ||
model.addAttribute("passwordMsg", map.get("passwordMsg")); | ||
return "/site/forget"; | ||
} | ||
} | ||
``` | ||
|
||
3. login增加超链接: | ||
|
||
```html | ||
<a th:href="@{/forget}" class="text-danger float-right">忘记密码?</a> | ||
``` | ||
|
||
forget.html: | ||
|
||
```html | ||
<!doctype html> | ||
<html lang="en" xmlns:th="https://www.thymeleaf.org"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
<meta name="_csrf" th:content="${_csrf.token}"> | ||
<meta name="_csrf_header" th:content="${_csrf.headerName}"> | ||
<link rel="icon" th:href="@{/img/icon.png}"/> | ||
<link rel="stylesheet" type="text/css" th:href="@{/css/bootstrap.min.css}" /> | ||
<link rel="stylesheet" th:href="@{/css/global.css}" /> | ||
<link rel="stylesheet" th:href="@{/css/login.css}" /> | ||
<title>忘记密码</title> | ||
</head> | ||
<body> | ||
<div class="nk-container"> | ||
<!-- 头部 --> | ||
<header class="bg-dark sticky-top" th:replace="index::header"> | ||
</header> | ||
|
||
<!-- 内容 --> | ||
<div class="main"> | ||
<div class="container pl-5 pr-5 pt-3 pb-3 mt-3 mb-3"> | ||
<form class="mt-5" th:action="@{/forgetPassword}" method="post"> | ||
<div class="form-group row"> | ||
<label for="your-email" class="col-sm-2 col-form-label text-right">邮箱:</label> | ||
<div class="col-sm-10"> | ||
<input type="email" th:class="|form-control ${emailMsg!=null?'is-invalid':''}|" | ||
th:value="${param.email}" | ||
id="your-email" placeholder="请输入您的邮箱!" name="email" required> | ||
<div class="invalid-feedback" th:text="${emailMsg}"> | ||
该邮箱已被注册! | ||
</div> | ||
</div> | ||
</div> | ||
<div class="form-group row mt-4"> | ||
<label for="your-password" class="col-sm-2 col-form-label text-right">新密码:</label> | ||
<div class="col-sm-10"> | ||
<input type="password" th:class="|form-control ${passwordMsg!=null?'is-invalid':''}|" | ||
th:value="${param.password}" | ||
id="your-password" placeholder="请输入新的密码!" name="password" required> | ||
<div class="invalid-feedback" th:text="${passwordMsg}"> | ||
密码长度不能小于8位! | ||
</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> | ||
</div> | ||
</div> | ||
|
||
<!-- 尾部 --> | ||
<footer class="bg-dark" th:replace="index::footer"> | ||
</footer> | ||
</div> | ||
|
||
<script th:src="@{/js/jquery-3.1.0.min.js}"></script> | ||
<script type="module" th:src="@{/js/popper.min.js}"></script> | ||
<script th:src="@{/js/bootstrap.min.js}"></script> | ||
<script th:src="@{/js/global.js}"></script> | ||
</body> | ||
</html> | ||
``` |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters