Reids存储邮箱验证码
先集成Redis
邮箱验证
授权码是QQ邮箱推出的,用于登录第三方客户端的专用密码。
温馨提醒:为了你的帐户安全,更改QQ密码以及独立密码会触发授权码过期,需要重新获取新的授权码登录。
1.QQ邮箱开启 POP3/SMTP服务
获取授权码
2.在spingboot中配置
1. 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2. springboot集成邮件
spring:
mail:
host: smtp.qq.com
username: 2811662803 # 发送方qq号
password: xhwyoxrqvtohddff # 授权码
default-encoding: utf-8
3. 注入 JavaMailSender 对象
@Autowired
private JavaMailSender mailSender;
4. 随机生成6位数字验证码
Random random = new Random();
//生成0-1000000之内的随机数
int randomNum = random.nextInt(1000000);
//不足6位补0
String randomCode = String.format("%06d", randomNum);
5. 发送邮件的逻辑代码
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("发送方QQ号@qq.com");
message.setTo("接收方QQ号@qq.com");
message.setSubject("主题:简单邮件验证");
message.setText("测试邮件内容");
mailSender.send(message);
实例测试
发送验证码
前端请求
//发送验证码
sendYzm(){
var _this = this;//this是vue对象
//正则验证邮箱是否合法
var email=this.form.account;
var reg = /^\w{5,}@[a-z0-9]{2,3}\.[a-z]+$|\,$/;
if(!reg.test(email)){
_this.$message({
message: "邮箱的格式不正确!",
type: 'warning'
});
}
this.$http.post("/api/login/sendYzm", this.form).then(function(response) {
if (response.data.code == 201) {
_this.$message({
message: response.data.msg,
type: 'warning'
});
return;
}
if (response.data.code == 200) {
_this.$message({
message: response.data.msg,
type: 'success'
});
return;
}
})
}
后端处理
Controller
@PostMapping(path = "/sendYzm")
public CommonResult sendYzm(@RequestBody MailCode mailCode) {//@RequestBody 接收请求体数据
service.createVerificationCode(mailCode.getAccount());
return new CommonResult(200, "发送成功",null);
}
Service
@Autowired
RedisTemplate redisTemplate;
@Autowired
JavaMailSender mailSender;
public void createVerificationCode(String account) {
//创建Redis简单string的操作类
ValueOperations operations = redisTemplate.opsForValue();
//生成简单的6位验证码
Random random = new Random();
int randomNum = random.nextInt(1000000);
String randomCode = String.format("%06d", randomNum);
//发送邮件
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("2811662803@qq.com");
message.setTo(account);
message.setSubject("主题:简单邮件验证");
message.setText("登录验证码:"+randomCode);
mailSender.send(message);
//设置键值对 k-v = 邮箱:验证码 的有效时间
operations.set(account, randomCode,1, TimeUnit.MINUTES);
}
校验 验证码
前端请求
//校验验证码
checkYzm(){
var _this = this;//this是vue对象
this.$http.post("/api/login/checkYzm",this.form).then(function(response) {
console.log(response.data);
if (response.data.code == 201) {
_this.$message({
message: response.data.msg,
type: "warning"
});
return;
}else if (response.data.code == 200) {
_this.$message({
message: response.data.msg,
type: "success"
});
return;
}else{
_this.$message({
message: response.data.msg,
type: "warning"
});
return;
}
})
}
后端处理
Controller
@PostMapping(path = "/checkYzm")
public CommonResult checkYzm(@RequestBody MailCode mailCode) {//@RequestBody 接收请求体数据
CommonResult res = null;
boolean bool = false;
try {
bool = service.checkCode(mailCode.getAccount(),mailCode.getCode());
res = new CommonResult(200, "验证成功",bool);
} catch (Exception e) {
res = new CommonResult(500, "验证失败",bool);
}
return res;
}
Service
public boolean checkCode(String account, String yzm) {
ValueOperations operations = redisTemplate.opsForValue();
String compare = (String)operations.get(account);
if(compare.equals(yzm)){
return true;
}
return false;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/15586.html