Skip to content

Commit c6b482f

Browse files
committed
added forget password
1 parent f6a3815 commit c6b482f

File tree

9 files changed

+199
-26
lines changed

9 files changed

+199
-26
lines changed
File renamed without changes.
File renamed without changes.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# 忘记密码
2+
3+
1. UserService:
4+
5+
```java
6+
// 重置密码 - forget
7+
public Map<String, Object> resetPassword(String email, String password) {
8+
Map<String, Object> map = new HashMap<>();
9+
if (StringUtils.isBlank(email)) {
10+
map.put("emailMsg", "邮箱不能为空!");
11+
return map;
12+
}
13+
if (StringUtils.isBlank(password)) {
14+
map.put("passwordMsg", "密码不能为空!");
15+
return map;
16+
}
17+
if(password.length()<8){
18+
map.put("passwordMsg", "密码不能少于8位!");
19+
return map;
20+
}
21+
User user = userMapper.selectByEmail(email);
22+
if (user == null) {
23+
map.put("emailMsg", "该邮箱不存在!");
24+
return map;
25+
}
26+
password = CommunityUtil.md5(password + user.getSalt());
27+
if (user.getPassword().equals(password)) {
28+
map.put("passwordMsg", "新密码不能和原密码相同!");
29+
return map;
30+
}
31+
userMapper.updatePassword(user.getId(), password);
32+
clearCache(user.getId());
33+
return map;
34+
}
35+
```
36+
37+
2. LoginController:
38+
39+
```java
40+
@RequestMapping(path = "/forget" ,method = RequestMethod.GET)
41+
public String getForgetPage(){
42+
return "/site/forget";
43+
}
44+
45+
@RequestMapping(path = "/forgetPassword", method = RequestMethod.POST)
46+
public String updatePassword(String email, String password ,Model model){
47+
Map<String, Object> map = userService.resetPassword(email, password);
48+
if (map == null || map.isEmpty()) {
49+
return "redirect:/logout";
50+
} else {
51+
model.addAttribute("emailMsg", map.get("emailMsg"));
52+
model.addAttribute("passwordMsg", map.get("passwordMsg"));
53+
return "/site/forget";
54+
}
55+
}
56+
```
57+
58+
3. login增加超链接:
59+
60+
```html
61+
<a th:href="@{/forget}" class="text-danger float-right">忘记密码?</a>
62+
```
63+
64+
forget.html:
65+
66+
```html
67+
<!doctype html>
68+
<html lang="en" xmlns:th="https://www.thymeleaf.org">
69+
<head>
70+
<meta charset="utf-8">
71+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
72+
<meta name="_csrf" th:content="${_csrf.token}">
73+
<meta name="_csrf_header" th:content="${_csrf.headerName}">
74+
<link rel="icon" th:href="@{/img/icon.png}"/>
75+
<link rel="stylesheet" type="text/css" th:href="@{/css/bootstrap.min.css}" />
76+
<link rel="stylesheet" th:href="@{/css/global.css}" />
77+
<link rel="stylesheet" th:href="@{/css/login.css}" />
78+
<title>忘记密码</title>
79+
</head>
80+
<body>
81+
<div class="nk-container">
82+
<!-- 头部 -->
83+
<header class="bg-dark sticky-top" th:replace="index::header">
84+
</header>
85+
86+
<!-- 内容 -->
87+
<div class="main">
88+
<div class="container pl-5 pr-5 pt-3 pb-3 mt-3 mb-3">
89+
<form class="mt-5" th:action="@{/forgetPassword}" method="post">
90+
<div class="form-group row">
91+
<label for="your-email" class="col-sm-2 col-form-label text-right">邮箱:</label>
92+
<div class="col-sm-10">
93+
<input type="email" th:class="|form-control ${emailMsg!=null?'is-invalid':''}|"
94+
th:value="${param.email}"
95+
id="your-email" placeholder="请输入您的邮箱!" name="email" required>
96+
<div class="invalid-feedback" th:text="${emailMsg}">
97+
该邮箱已被注册!
98+
</div>
99+
</div>
100+
</div>
101+
<div class="form-group row mt-4">
102+
<label for="your-password" class="col-sm-2 col-form-label text-right">新密码:</label>
103+
<div class="col-sm-10">
104+
<input type="password" th:class="|form-control ${passwordMsg!=null?'is-invalid':''}|"
105+
th:value="${param.password}"
106+
id="your-password" placeholder="请输入新的密码!" name="password" required>
107+
<div class="invalid-feedback" th:text="${passwordMsg}">
108+
密码长度不能小于8位!
109+
</div>
110+
</div>
111+
</div>
112+
<div class="form-group row mt-4">
113+
<div class="col-sm-2"></div>
114+
<div class="col-sm-10 text-center">
115+
<button type="submit" class="btn btn-info text-white form-control">重置密码</button>
116+
</div>
117+
</div>
118+
</form>
119+
</div>
120+
</div>
121+
122+
<!-- 尾部 -->
123+
<footer class="bg-dark" th:replace="index::footer">
124+
</footer>
125+
</div>
126+
127+
<script th:src="@{/js/jquery-3.1.0.min.js}"></script>
128+
<script type="module" th:src="@{/js/popper.min.js}"></script>
129+
<script th:src="@{/js/bootstrap.min.js}"></script>
130+
<script th:src="@{/js/global.js}"></script>
131+
</body>
132+
</html>
133+
```
File renamed without changes.
File renamed without changes.

src/main/java/com/nowcoder/community/controller/LoginController.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,21 @@ public String logout(@CookieValue("ticket") String ticket, HttpServletRequest re
176176
securityContextLogoutHandler.logout(request, response, authentication);
177177
return "redirect:/login";
178178
}
179+
180+
@RequestMapping(path = "/forget" ,method = RequestMethod.GET)
181+
public String getForgetPage(){
182+
return "/site/forget";
183+
}
184+
185+
@RequestMapping(path = "/forgetPassword", method = RequestMethod.POST)
186+
public String updatePassword(String email, String password ,Model model){
187+
Map<String, Object> map = userService.resetPassword(email, password);
188+
if (map == null || map.isEmpty()) {
189+
return "/site/login";
190+
} else {
191+
model.addAttribute("emailMsg", map.get("emailMsg"));
192+
model.addAttribute("passwordMsg", map.get("passwordMsg"));
193+
return "/site/forget";
194+
}
195+
}
179196
}

src/main/java/com/nowcoder/community/service/UserService.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,37 @@ public int updateHeader(int userId, String headerUrl){
190190
return rows;
191191
}
192192

193+
// 重置密码 - forget
194+
public Map<String, Object> resetPassword(String email, String password) {
195+
Map<String, Object> map = new HashMap<>();
196+
if (StringUtils.isBlank(email)) {
197+
map.put("emailMsg", "邮箱不能为空!");
198+
return map;
199+
}
200+
if (StringUtils.isBlank(password)) {
201+
map.put("passwordMsg", "密码不能为空!");
202+
return map;
203+
}
204+
if(password.length()<8){
205+
map.put("passwordMsg", "密码不能少于8位!");
206+
return map;
207+
}
208+
User user = userMapper.selectByEmail(email);
209+
if (user == null) {
210+
map.put("emailMsg", "该邮箱不存在!");
211+
return map;
212+
}
213+
password = CommunityUtil.md5(password + user.getSalt());
214+
if (user.getPassword().equals(password)) {
215+
map.put("passwordMsg", "新密码不能和原密码相同!");
216+
return map;
217+
}
218+
userMapper.updatePassword(user.getId(), password);
219+
clearCache(user.getId());
220+
return map;
221+
}
222+
223+
// 更新密码 - not forget
193224
public Map<String, Object> updatePassword(int userId, String oldPassword, String newPassword1, String newPassword2){
194225
Map<String, Object> map = new HashMap<>();
195226
User user = userMapper.selectById(userId);
Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,46 @@
11
<!doctype html>
2-
<html lang="en">
2+
<html lang="en" xmlns:th="https://www.thymeleaf.org">
33
<head>
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
66
<meta name="_csrf" th:content="${_csrf.token}">
77
<meta name="_csrf_header" th:content="${_csrf.headerName}">
88
<link rel="icon" th:href="@{/img/icon.png}"/>
99
<link rel="stylesheet" type="text/css" th:href="@{/css/bootstrap.min.css}" />
10-
<link rel="stylesheet" href="../css/global.css" />
11-
<link rel="stylesheet" href="../css/login.css" />
12-
<title>牛客网-忘记密码</title>
10+
<link rel="stylesheet" th:href="@{/css/global.css}" />
11+
<link rel="stylesheet" th:href="@{/css/login.css}" />
12+
<title>忘记密码</title>
1313
</head>
1414
<body>
1515
<div class="nk-container">
1616
<!-- 头部 -->
17-
<header class="bg-dark sticky-top">
17+
<header class="bg-dark sticky-top" th:replace="index::header">
1818
</header>
1919

2020
<!-- 内容 -->
2121
<div class="main">
2222
<div class="container pl-5 pr-5 pt-3 pb-3 mt-3 mb-3">
23-
<form class="mt-5">
23+
<form class="mt-5" th:action="@{/forgetPassword}" method="post">
2424
<div class="form-group row">
2525
<label for="your-email" class="col-sm-2 col-form-label text-right">邮箱:</label>
2626
<div class="col-sm-10">
27-
<input type="email" class="form-control" id="your-email" placeholder="请输入您的邮箱!" required>
28-
<div class="invalid-feedback">
27+
<input type="email" th:class="|form-control ${emailMsg!=null?'is-invalid':''}|"
28+
th:value="${param.email}"
29+
id="your-email" placeholder="请输入您的邮箱!" name="email" required>
30+
<div class="invalid-feedback" th:text="${emailMsg}">
2931
该邮箱已被注册!
3032
</div>
3133
</div>
3234
</div>
33-
<div class="form-group row mt-4">
34-
<label for="verifycode" class="col-sm-2 col-form-label text-right">验证码:</label>
35-
<div class="col-sm-6">
36-
<input type="text" class="form-control" id="verifycode" placeholder="请输入验证码!">
37-
<div class="invalid-feedback">
38-
验证码不正确!
39-
</div>
40-
</div>
41-
<div class="col-sm-4">
42-
<a href="javascript:;" class="btn btn-info form-control">获取验证码</a>
43-
</div>
44-
</div>
4535
<div class="form-group row mt-4">
4636
<label for="your-password" class="col-sm-2 col-form-label text-right">新密码:</label>
4737
<div class="col-sm-10">
48-
<input type="password" class="form-control" id="your-password" placeholder="请输入新的密码!" required>
49-
<div class="invalid-feedback">
38+
<input type="password" th:class="|form-control ${passwordMsg!=null?'is-invalid':''}|"
39+
th:value="${param.password}"
40+
id="your-password" placeholder="请输入新的密码!" name="password" required>
41+
<div class="invalid-feedback" th:text="${passwordMsg}">
5042
密码长度不能小于8位!
51-
</div>
43+
</div>
5244
</div>
5345
</div>
5446
<div class="form-group row mt-4">
@@ -62,13 +54,13 @@
6254
</div>
6355

6456
<!-- 尾部 -->
65-
<footer class="bg-dark">
57+
<footer class="bg-dark" th:replace="index::footer">
6658
</footer>
6759
</div>
6860

6961
<script th:src="@{/js/jquery-3.1.0.min.js}"></script>
7062
<script type="module" th:src="@{/js/popper.min.js}"></script>
7163
<script th:src="@{/js/bootstrap.min.js}"></script>
72-
<script src="../js/global.js"></script>
64+
<script th:src="@{/js/global.js}"></script>
7365
</body>
7466
</html>

src/main/resources/templates/site/login.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ <h3 class="text-center text-info border-bottom pb-3">登录</h3>
6565
name="rememberme" id="remember-me"
6666
th:checked="${param.rememberme}">
6767
<label class="form-check-label" for="remember-me">记住我</label>
68-
<a href="forget.html" class="text-danger float-right">忘记密码?</a>
68+
<a th:href="@{/forget}" class="text-danger float-right">忘记密码?</a>
6969
</div>
7070
</div>
7171
<div class="form-group row mt-4">

0 commit comments

Comments
 (0)