先集成Redis
授权码是QQ邮箱推出的,用于登录第三方客户端的专用密码。
温馨提醒:为了你的帐户安全,更改QQ密码以及独立密码会触发授权码过期,需要重新获取新的授权码登录。
POP3/SMTP服务
获取授权码进入QQ邮箱,进入设置->账户
下拉 开启POP3/STMP服务
验证密保
获取授权码
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
spring:
mail:
host: smtp.qq.com
username: 2811662803 # 发送方qq号
password: xhwyoxrqvtohddff # 授权码
default-encoding: utf-8
@Autowired
private JavaMailSender mailSender;
Random random = new Random();
//生成0-1000000之内的随机数
int randomNum = random.nextInt(1000000);
//不足6位补0
String randomCode = String.format("%06d", randomNum);
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;
}
正则验证邮箱