springboot实现邮箱验证码功能
本文实例为大家分享了springboot实现邮箱验证码功能的具体代码,供大家参考,具体内容如下:
以我的qq为例
第一步:开通qq POP3/SMTP服务和IMAP/SMTP服务
*
第二步:导入maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
第三步:properties文件配置文件
#邮件发送配置
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.qq.com
spring.mail.username=你的qq邮箱
spring.mail.password=你的qq授权码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
第四步 编写MsmServiceImpl
package com.lzb.msmservice.service.impl;
import com.lzb.msmservice.service.MsmService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@Service
public class MsmServiceImpl implements MsmService {
@Autowired
JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String username;
Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public boolean sent(String email, String title, String code) {
if(StringUtils.isEmpty(email)) return false;
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(username);
message.setTo(email);
message.setSubject(title);
message.setText(code);
mailSender.send(message);
logger.info("邮件发送成功");
return true;
}
}
第五步 编写MsmController
package com.lzb.msmservice.controller;
import com.lzb.commonutils.R;
import com.lzb.msmservice.service.MsmService;
import com.lzb.msmservice.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/edumsm/msm")
@CrossOrigin
public class MsmController {
@Autowired
private MsmService msmService;
@Autowired
RedisTemplate<String,String> redisTemplate;
@GetMapping("/sentEmail/{email}")
public R sentEmail(@PathVariable String email){
String code = redisTemplate.opsForValue().get(email);
if(!StringUtils.isEmpty(code)) return R.ok();
code = RandomUtil.getFourBitRandom();
boolean flag = msmService.sent(email, "注册验证码", code);
if(flag){
redisTemplate.opsForValue().set(email,code,1, TimeUnit.MINUTES);
return R.ok();
}else {
return R.error().message("短信发送失败");
}
}
}
第六步 编写验证码的生成文件
package com.atguigu.msmservice.util;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
/**
* 获取随机数
*
* @author qianyi
*
*/
public class RandomUtil {
private static final Random random = new Random();
private static final DecimalFormat fourdf = new DecimalFormat("0000");
private static final DecimalFormat sixdf = new DecimalFormat("000000");
public static String getFourBitRandom() {
return fourdf.format(random.nextInt(10000));
}
public static String getSixBitRandom() {
return sixdf.format(random.nextInt(1000000));
}
/**
* 给定数组,抽取n个数据
* @param list
* @param n
* @return
*/
public static ArrayList getRandom(List list, int n) {
Random random = new Random();
HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
// 生成随机数字并存入HashMap
for (int i = 0; i < list.size(); i++) {
int number = random.nextInt(100) + 1;
hashMap.put(number, i);
}
// 从HashMap导入数组
Object[] robjs = hashMap.values().toArray();
ArrayList r = new ArrayList();
// 遍历数组并打印数据
for (int i = 0; i < n; i++) {
r.add(list.get((int) robjs[i]));
System.out.print(list.get((int) robjs[i]) + "\t");
}
System.out.print("\n");
return r;
}
}