Skip to content

Commit

Permalink
added forget password
Browse files Browse the repository at this point in the history
  • Loading branch information
1517005260 committed Jun 4, 2024
1 parent f6a3815 commit c6b482f
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 26 deletions.
File renamed without changes.
133 changes: 133 additions & 0 deletions help/2-LoginAndRegister/7-AccountSetting/7.2-ForgetPassword.md
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.
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,21 @@ public String logout(@CookieValue("ticket") String ticket, HttpServletRequest re
securityContextLogoutHandler.logout(request, response, authentication);
return "redirect:/login";
}

@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 "/site/login";
} else {
model.addAttribute("emailMsg", map.get("emailMsg"));
model.addAttribute("passwordMsg", map.get("passwordMsg"));
return "/site/forget";
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/nowcoder/community/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,37 @@ public int updateHeader(int userId, String headerUrl){
return rows;
}

// 重置密码 - 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;
}

// 更新密码 - not forget
public Map<String, Object> updatePassword(int userId, String oldPassword, String newPassword1, String newPassword2){
Map<String, Object> map = new HashMap<>();
User user = userMapper.selectById(userId);
Expand Down
42 changes: 17 additions & 25 deletions src/main/resources/templates/site/forget.html
Original file line number Diff line number Diff line change
@@ -1,54 +1,46 @@
<!doctype html>
<html lang="en">
<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" href="../css/global.css" />
<link rel="stylesheet" href="../css/login.css" />
<title>牛客网-忘记密码</title>
<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">
<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">
<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" class="form-control" id="your-email" placeholder="请输入您的邮箱!" required>
<div class="invalid-feedback">
<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="verifycode" class="col-sm-2 col-form-label text-right">验证码:</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="verifycode" placeholder="请输入验证码!">
<div class="invalid-feedback">
验证码不正确!
</div>
</div>
<div class="col-sm-4">
<a href="javascript:;" class="btn btn-info form-control">获取验证码</a>
</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" class="form-control" id="your-password" placeholder="请输入新的密码!" required>
<div class="invalid-feedback">
<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>
<div class="form-group row mt-4">
Expand All @@ -62,13 +54,13 @@
</div>

<!-- 尾部 -->
<footer class="bg-dark">
<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 src="../js/global.js"></script>
<script th:src="@{/js/global.js}"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion src/main/resources/templates/site/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ <h3 class="text-center text-info border-bottom pb-3">登录</h3>
name="rememberme" id="remember-me"
th:checked="${param.rememberme}">
<label class="form-check-label" for="remember-me">记住我</label>
<a href="forget.html" class="text-danger float-right">忘记密码?</a>
<a th:href="@{/forget}" class="text-danger float-right">忘记密码?</a>
</div>
</div>
<div class="form-group row mt-4">
Expand Down

0 comments on commit c6b482f

Please sign in to comment.