申请应用程序验证码
配置二步验证
要配置二步验证,才能看到应用程序的选项
最后就能获取到应用程序验证码
引入代码
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
|
application.properties文件配置
1 2 3 4 5 6 7 8 9 10 11 12 13
| spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=你的Gmail邮箱账号
spring.mail.password=应用程序专用密码
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.transport.protocol=smtp
|
配置的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| @Service public class EmailUtil implements EmailService { private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired private JavaMailSender mailSender;
@Value("${spring.mail.username}") private String from;
@Override public void sendSimpleMail(String to, String subject, String content) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setText(content); mailSender.send(message); }
@Override public void sendHtmlMail(String to, String subject, String content) { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper messageHelper; try { messageHelper = new MimeMessageHelper(message, true); messageHelper.setFrom(from); InternetAddress[] internetAddressTo = InternetAddress.parse(to); messageHelper.setTo(internetAddressTo); message.setSubject(subject); messageHelper.setText(content, true); mailSender.send(message); logger.info("邮件已经发送。"); } catch (Exception e) { logger.error("发送邮件时发生异常!", e); } }
@Override public void sendAttachmentsMail(String to, String subject, String content, String filePath) { MimeMessage message = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); mailSender.send(message); logger.info("邮件已经发送。"); } catch (Exception e) { logger.error("发送邮件时发生异常!", e); } }
public boolean isEmail(String email) { if (email == null || email.length() < 1 || email.length() > 256) { return false; } Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"); return pattern.matcher(email).matches(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public interface EmailService {
void sendSimpleMail(String to, String subject, String content);
public void sendHtmlMail(String to, String subject, String content);
public void sendAttachmentsMail(String to, String subject, String content, String filePath); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @Service public class CodeUtil {
@Autowired private StringRedisTemplate redisTemplate;
public String generateVerificationCode() { return String.valueOf((int) ((Math.random() * 9 + 1) * 100000)); }
public boolean verifyCode(String userId, String code,String key) { key = key + userId; String storedCode = redisTemplate.opsForValue().get(key); return code.equals(storedCode); }
public void deleteCode(String userId,String key) { key = key + userId; redisTemplate.delete(key); } }
|
1 2 3 4 5 6 7
|
private static final int EXPIRATION_TIME_IN_MINUTES = 3; private static final String KEY_PREFIX =="xxx"; key=KEY_PREFIX+"123456" redisTemplate.opsForValue().set(key, 随机数字验证码, EXPIRATION_TIME_IN_MINUTES, TimeUnit.MINUTES);
|