方特網(wǎng)站是誰(shuí)做的seo案例視頻教程
目錄
1、開(kāi)啟郵箱IMAP/SMTP服務(wù),獲取授權(quán)碼
2、相關(guān)代碼
? ? ? ? 1、使用配置Redis(用于存儲(chǔ)驗(yàn)證碼,具有時(shí)效性)
? ? ? ? 2、郵箱依賴(lài)和hutool(用于隨機(jī)生成驗(yàn)證碼)
? ? ? ? 3、配置Redis和郵箱信息
? ? ? ? 4、開(kāi)啟Redis服務(wù)
? ? ? ? 5、編寫(xiě)發(fā)送郵箱驗(yàn)證碼
? ? ? ? 6、郵箱登錄驗(yàn)證功能
3、測(cè)試
1、開(kāi)啟郵箱IMAP/SMTP服務(wù),獲取授權(quán)碼
? ? ? ? 1、登錄郵箱(以qq郵箱為例),點(diǎn)擊郵箱右上角郵箱-設(shè)置-常規(guī)-第三方服務(wù),會(huì)有如下選項(xiàng)
? ? ? ? 2、點(diǎn)擊生成授權(quán)碼(這邊已經(jīng)開(kāi)啟IMAP/SMTP服務(wù),未開(kāi)啟的要先設(shè)置為開(kāi)啟),之后根據(jù)指示可以拿到一串由郵箱服務(wù)器提供的專(zhuān)屬于自己的授權(quán)碼,用于在Java代碼中連接郵箱
2、相關(guān)代碼
? ? ? ? 1、使用配置Redis(用于存儲(chǔ)驗(yàn)證碼,具有時(shí)效性)
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
? ? ? ? 2、郵箱依賴(lài)和hutool(用于隨機(jī)生成驗(yàn)證碼)
<!-- 郵箱驗(yàn)證碼依賴(lài) --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency><!-- 一個(gè)很強(qiáng)大的工具庫(kù) --><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.19</version></dependency>
? ? ? ? 3、配置Redis和郵箱信息
redis:host: localhostport: 6379mail:host: smtp.qq.comprotocol: smtpdefault-encoding: UTF-8username: 【這里填發(fā)件人郵箱,可以是你自己的】password: 【這里填剛剛從郵箱網(wǎng)站中拿到的授權(quán)碼,注意不是自己的郵箱密碼!!】nickname: 【發(fā)件人自命名】properties:mail.smtp.auth: true #啟用SMTP服務(wù)器的身份驗(yàn)證,這是為了確保只有合法用戶(hù)可以發(fā)送郵件。mail.smtp.starttls.enable: #啟用TLS加密,這是為了確保郵件傳輸?shù)陌踩?。mail.smtp.starttls.required: true #要求使用TLS加密,如果連接不是通過(guò)TLS加密傳輸,則連接將失敗。
? ? ? ? 4、開(kāi)啟Redis服務(wù)
? ? ? ? 這里以windows作為舉例:
? ? ? ? 5、編寫(xiě)發(fā)送郵箱驗(yàn)證碼
import cn.hutool.core.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.concurrent.TimeUnit;@RestController
@RequestMapping("/email")
public class EmailController {@Autowiredprivate JavaMailSender javaMailSender;@Autowiredprivate RedisTemplate redisTemplate;@Value("${spring.mail.username}")private String sender;@Value("${spring.mail.nickname}")private String nickname;@GetMapping("/code")public String getCode(@RequestParam("email") String email){// 創(chuàng)建一個(gè)郵件SimpleMailMessage message = new SimpleMailMessage();// 設(shè)置發(fā)件人message.setFrom(nickname+'<'+sender+'>');// 設(shè)置收件人message.setTo(email);// 設(shè)置郵件主題message.setSubject("歡迎訪(fǎng)問(wèn)"+nickname);//生成六位隨機(jī)數(shù)String code = RandomUtil.randomNumbers(6);//將驗(yàn)證碼存入redis,有效期為5分鐘redisTemplate.opsForValue().set("email_code_"+email, code, 300000, TimeUnit.MILLISECONDS);String content = "【驗(yàn)證碼】您的驗(yàn)證碼為:" + code + " 。 驗(yàn)證碼五分鐘內(nèi)有效,逾期作廢。\n\n\n" +"------------------------------\n\n\n" ;message.setText(content);// 發(fā)送郵件javaMailSender.send(message);return "發(fā)送成功";}}
? ? ? ? 6、郵箱登錄驗(yàn)證功能
@RestController
@RequestMapping("/user")
public class UserController {@Resourceprivate RedisTemplate redisTemplate;// 通過(guò)郵箱驗(yàn)證登錄@PostMapping("/loginByEmail")public ResponseBean loginByEmail(@RequestParam(value = "code") String code) {String email = "【這里寫(xiě)你已經(jīng)收到驗(yàn)證碼的郵箱】";String emailKey = "email_code_"+email;String storedToken = (String) redisTemplate.opsForValue().get(emailKey);if(code.equals(storedToken)){return ResponseBean.success("驗(yàn)證成功",null);}else {return ResponseBean.error("驗(yàn)證失敗");}}}
3、測(cè)試
?????????? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 成功接收到來(lái)自發(fā)送方的驗(yàn)證碼
再進(jìn)行郵箱登錄測(cè)試
? ??
? ? ? ? 檢查Redis服務(wù)器存儲(chǔ)情況: