SpringBoot实战项⽬(⼗五)--修改密码及登录退出功能实现
(⽤户名从session获取,然后根据⽤户名查询对应user信息做验证)
页⾯构建
1<!DOCTYPE html>
2<html class="x-admin-sm" xmlns:th="">
3<head>
4<meta charset="UTF-8">
5<title>修改密码</title>
6<header th:replace="header.html"></header>
7<body>
8<div class="layui-fluid">
9<div class="layui-row">
10<form class="layui-form">
11<div class="layui-form-item">
12<label for="L_username" class="layui-form-label">⽤户名</label>
13<div class="layui-input-inline">
14<input type="text" id="L_username" name="userName" disabled="" th:value="${session.user.userName}" class="layui-input">
15</div>
16<div class="layui-form-mid layui-word-aux">
17<span class="x-red">*</span>将会成为您唯⼀的登⼊名
18</div>
19</div>
20<div class="layui-form-item">
21<label for="L_pass" class="layui-form-label">
22<span class="x-red">*</span>旧密码</label>
23<div class="layui-input-inline">
24<input type="password" id="O_pass" name="oldPassword" lay-verify="oldPassword" autocomplete="off" class="layui-input">
25</div>
26<div class="layui-form-mid layui-word-aux">6到16个字符</div>
27</div>
28<div class="layui-form-item">
29<label for="L_pass" class="layui-form-label">
30<span class="x-red">*</span>新密码</label>
31<div class="layui-input-inline">
32<input type="password" id="L_pass" name="newPassword" lay-verify="newPassword" autocomplete="off" class="layui-input"></div>
33<div class="layui-form-mid layui-word-aux">6到16个字符</div>
34</div>
35<div class="layui-form-item">
36<label for="L_repass" class="layui-form-label">
37<span class="x-red">*</span>确认密码</label>
38<div class="layui-input-inline">
39<input type="password" id="L_repass" name="newPassword2" required="" lay-verify="newPassword2" autocomplete="off" class="layui-input"> 40</div>
41<div class="layui-form-mid layui-word-aux">6到16个字符</div>
42</div>
43<div class="layui-form-item">
44<label for="L_repass" class="layui-form-label"></label>
45<button class="layui-btn" lay-filter="save" lay-submit="">修改</button></div>
46</form>
47</div>
48</div>
49<script>
50      layui.use(['form', 'layer'],
51function() {
52                $ = layui.jquery;
53var form = layui.form,
54                layer = layui.layer;
55
56//⾃定义验证规则
57            form.verify({
58            oldPassword: [/(.+){5,12}$/, '请输⼊旧密码!'],
59            newPassword: [/(.+){5,12}$/, '请输⼊新密码!'],
60            pass: [/(.+){5,12}$/, '密码必须6到12位']
61            ,newPassword2: function(value){
62if($('#L_pass').val()!=$('#L_repass').val()){
63return '两次密码不⼀致';
64                }
65            }
66        });
67
68//监听提交
69                ('submit(save)',function(data) {
70                    $.ajax({
71                        url:"/user/changePassword",
72                        type:"POST",
73                        data:data.field,
74                        dataType:"json",
75                        success:function (result) {
76var msg="修改密码成功!";
77if (de !=200){
78                                  layer.alert(result.msg);
79                            }else {
80                                  layer.alert(msg,{icon:6},function () {
81// 获得frame索引
82var index = FrameIndex(window.name);
83//关闭当前frame
84                            parent.layer.close(index);
85                            });
86
87                            }
88                        }
89                    });
90return false;
91                });
92            });
93</script>
94</body>
95</html>
UserController
1/**
2    * 修改密码功能实现
3    * @param userName
4    * @param oldPassword
5    * @param newPassword
6
7    * @return
8*/
9    @PostMapping("/changePassword")
10    @ResponseBody
11public Results changePassword(@RequestParam("userName") String userName,
12                                            @RequestParam("oldPassword") String oldPassword,
13                                            @RequestParam("newPassword") String newPassword){
14        SysUser user = userService.findByUserByUserName(userName);
15if (user == null){
16return Results.failure(1,"⽤户不存在!");
17        }
18if (!ptWithSalt(oldPassword).PassWord())){
19return Results.failure(1,"旧密码错误!");
20        }
21return userService.changePassword(userName,newPassword);
22    }
UserService
Results changePassword(String userName, String newPassword); UserServiceImpl
1  @Override
2public Results changePassword(String userName,String newPassword) {
3        userDao.changePassword(ptWithSalt(newPassword));
4return Results.success();
5    }
UserDao
int changePassword(@Param("userName") String userName,@Param("password") String password); l
1 <!--修改密码-->
2    <update id="changePassword">
密码修改3        update sys_user set password=#{password} where user_name = #{userName}
4    </update>
测试----
退出功能⽐较简单--移除session保存的user,重定向到登录页⾯ LoginController
1    @GetMapping("/exit")
2public String exit(HttpServletRequest request){
3        Session().removeAttribute("user");
4return "redirect:/login";
5    }