实现的功能:
通过邮箱发送链接到qq邮箱,来修改管理员的密码
controller层:
//修改管理员密码
@PostMapping("/updatepwd")
public String updatepwd(Integer id,String password,String newpassword){
loginServiceImpl.updatepassword(id,password,newpassword);
return "index";
}
//填写邮箱
@PostMapping("/send")
public String send(String email,Model model) throws MessagingException {
boolean i=false;
i=loginServiceImpl.sendemail(email);
return "emp/sendsuccess";
}
@GetMapping("/sendemail")
public String updatepwd(){
return "emp/sendemail";
}
@GetMapping("/toupdate")
public String toupdate(){
return "emp/updatepwd";
}
}
service层
@Override
public void updatepassword(Integer id, String pwd,String newpwd) {
Admin admin = adminMapper.findAdminByIdAndPwd(id,pwd);
admin.setPassword(newpwd);
int i = adminMapper.updateByPassword(admin);
}
@Override
public boolean sendemail(String email) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setTo(email);
helper.setFrom("**********@qq.com");//填自己的邮箱
helper.setSubject("管理系统修改密码");
mimeMessage.setContent("<a href=\"http://localhost:8080/toupdate\">点击链接进入修改页面</a>","text/html;charset=utf8");
javaMailSender.send(mimeMessage);
return true;
}
}
application.properties
spring.mail.username=qq邮箱地址
spring.mail.password=自己的邮箱码
前端代码:具体可以看我的gitee代码
<body>
<!--顶部导航栏-->
<div th:replace="~{commons/commons::topbar}"></div>
<div class="container-fluid">
<div class="row">
<!--侧边栏-->
<div th:replace="~{commons/commons::sidebar(active='list')}"></div>
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
<h2>修改管理员密码</h2>
<div style="width: 350px">
<form th:action="@{/send}" method="post">
<div class="form-group">
<label>我们将通过邮箱发送一个链接地址,打开链接进行修改密码的操作</label>
<input type="text" class="form-control" placeholder="请输入邮箱" name="email">
</div>
<button type="submit" class="btn btn-primary">点击发送</button>
</form>
</div>
</main>
</div>
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script type="text/javascript" th:src="@{/js/jquery-3.2.1.slim.min.js}"></script>
<script type="text/javascript" th:src="@{/js/popper.min.js}"></script>
<script type="text/javascript" th:src="@{/js/bootstrap.min.js}"></script>
<!-- Icons -->
<script type="text/javascript" th:src="@{/js/feather.min.js}"></script>
<script>
feather.replace()
</script>
<!-- Graphs -->
<script type="text/javascript" th:src="@{/js/Chart.min.js}"></script>
<script>
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
datasets: [{
data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],
lineTension: 0,
backgroundColor: 'transparent',
borderColor: '#007bff',
borderWidth: 4,
pointBackgroundColor: '#007bff'
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: false
}
}]
},
legend: {
display: false,
}
}
});
</script>
</body>
但是这个邮箱发送时间较慢,后期再优化
代码地址
gitee地址:链接: 跳转到gitee项目仓库.