1. First configure redis, the author uses stringRedisTemplate
You can check my previous blog, springboot configuration stringRedisTemplate
2. User registration and password retrieval are all achieved by email
So you need to configure the mailbox to send mail normally, see the details: SpringBoot uses JavaMailSender to send mail
3. Use a random number to generate a four-digit random number as a verification code, and set the expiration time to three minutes,
String emailReg = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
Pattern pattern = Pattern.compile(emailReg);
Matcher matcher = pattern.matcher(email);
if(matcher.find()){ //验证邮箱格式是否正确
String code = String.valueOf((int)((Math.random()*9+1)*1000)); //四位随机数验证码
//向redis里存入数据和设置缓存时间
redisTemplate.opsForValue().set(email, code, 60 * 3, TimeUnit.SECONDS);
try {
emailService.sendEmail("123456***@163.com",email,"验证码",code);
data.put("code","success");
data.put("msg","验证码发送成功,三分钟有效,请注意查收!");
} catch (Exception e) {
e.printStackTrace();
data.put("code","error");
data.put("msg","无法发送邮件,请稍后重试!");
}
}else {
data.put("code","error");
data.put("msg","邮箱格式不正确!");
}