springboot項目接入第三方qq郵箱驗證登錄的全過程
前言:
本次項目基于springboot 3.x
jdk為17
Maven版本為:3.8.1
IDEA版本為:2023.3.x
1、在qq郵箱進(jìn)行相關(guān)的設(shè)置
第一步:我們進(jìn)入qq郵箱的右上角有一個賬號與安全設(shè)置:我們點進(jìn)去
第二步:點進(jìn)去后來到這個頁面:選擇安全設(shè)置,在最下面有這個服務(wù),我們需要開啟這個服務(wù),然后生成一個授權(quán)碼,生成的授權(quán)碼記得保存下來,這個很有用處
2、編寫java代碼
緊接著在我們的項目工程中就可以編寫java代碼了
2.1、首先我們先引入相關(guān)的maven依賴包
首先在我們的pom.xml文件中引入關(guān)于email的依賴包
<!-- email郵箱驗證--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2.2、隨后配置yml文件,進(jìn)行相關(guān)的配置
spring: mail: host: smtp.qq.com # 指定郵件服務(wù)器的主機(jī)名,這里默認(rèn)是smtp.qq.com,表示使用的是騰訊的SMTP服務(wù)器。 port: 587 # 456也行 username: "你的email" # xxxx@qq.com 這里是發(fā)送者 password: "授權(quán)碼" # 這里就是我前面提到的授權(quán)碼了 properties: # 其他配置 mail: smtp: socketFactoryClass: javax.net.ssl.SSLSocketFactory # 指定SSL Socket工廠類,用于創(chuàng)建加密的郵件連接。 auto: true # 設(shè)置為true表示啟用自動連接。 starttls: # 配置STARTTLS加密連接 enable: true # 設(shè)置為true表示啟用STARTTLS。 required: true # 設(shè)置為true表示STARTTLS是必需的,如果不可用,則會拋出異常。 default-encoding: UTF-8 # 設(shè)置郵件內(nèi)容的默認(rèn)編碼格式為UTF-8 默認(rèn)就是UTF-8
上面就是配置基本信息了,根據(jù)你的需求可以自行更改
2.3、創(chuàng)建Email工具類
我們需要將業(yè)務(wù)代碼整合到一個工具類(EmailSendUtils )中進(jìn)行編碼,我們創(chuàng)建一個專門用于發(fā)送email郵箱驗證碼的類,里面涵蓋了發(fā)送的步驟,以及隨機(jī)生成6位字母和數(shù)字的驗證碼。
上代碼:
package com.sxy.recordnetwork.Utils; import com.sxy.recordnetwork.enumeration.Constants; import org.aspectj.apache.bcel.classfile.Code; 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.stereotype.Service; import javax.swing.*; import java.util.Arrays; import java.util.concurrent.TimeUnit; import java.util.stream.Stream; /** * 發(fā)送郵箱驗證碼工具類 * Created by sxy on 2024/3/11. */ @Service public class EmailSendUtils { // 注入JavaMailSender接口 @Autowired private JavaMailSender mailSender; // 集成redis進(jìn)行驗證碼的緩存 @Autowired private RedisTemplate redisTemplate; // 通過value注解得到配置文件中發(fā)送者的郵箱 @Value("${spring.mail.username}") private String userName;// 用戶發(fā)送者 // 郵箱驗證碼 定義為StringBuilder對于增刪改操作有優(yōu)勢 private final StringBuilder EMAIL_CODE = new StringBuilder(); // 創(chuàng)建一個發(fā)送郵箱驗證的方法 public void sendVerificationEmail(String to){ try{ // 定義email信息格式 SimpleMailMessage message = new SimpleMailMessage(); // 調(diào)用生成6位數(shù)字和字母的方法,生成驗證碼,該方法在下面定義好了 generateRandomCode(); // 設(shè)置發(fā)件人 message.setFrom(userName); // 接收者郵箱,為調(diào)用本方法傳入的接收者的郵箱xxx@qq.com message.setTo(to); // 郵件主題 message.setSubject(Constants.EMAIL_TITLE.getValue()); // 郵件內(nèi)容 設(shè)置的郵件內(nèi)容,這里我使用了常量類字符串,加上驗證碼,再加上常量類字符串 message.setText(Constants.EMAIL_MESSAGE.getValue()+EMAIL_CODE+Constants.EMAIL_OUTTIME_TEN.getValue()); // 開始發(fā)送 mailSender.send(message); }catch (Exception e){ e.printStackTrace(); }finally{ // 發(fā)送完了之后,將EMAIL_CODE設(shè)置為空 EMAIL_CODE.setLength(0); } } /** * 隨機(jī)生成6位字母加數(shù)字組合的驗證碼 * @return */ public void generateRandomCode(){ // 字母和數(shù)字組合 String str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; // 拆分每一個字符放到數(shù)組中 String[] newStr = str.split(""); // 循環(huán)隨機(jī)生成6為數(shù)字和字母組合 for (int i = 0; i < 6; i++){ // 通過循環(huán)6次,為stringBuilder追加內(nèi)容,內(nèi)容為隨機(jī)數(shù)?62,取整 EMAIL_CODE.append(newStr[(int)(Math.random() * 62)]); } //TODO 這里存儲的key如果多個用戶同時發(fā)送的話會覆蓋key,就會導(dǎo)致第一個人的驗證碼被覆蓋 // 存入Redis中并設(shè)置時長為2分鐘 redisTemplate.opsForValue().set(Constants.EMAIL_CODE.getValue(), EMAIL_CODE,120, TimeUnit.SECONDS); } }
這里我聲明了一個自定義異常類BaseException去繼承RuntimeException這個類
package com.sxy.recordnetwork.Exception; public class BaseException extends RuntimeException{ public BaseException( String msg) { super(msg); } }
然后我們再創(chuàng)建驗證碼相關(guān)的異常類信息去繼承這個就可以了
package com.sxy.recordnetwork.Exception.Son; import com.sxy.recordnetwork.Exception.BaseException; /** * 郵箱驗證碼異常 */ public class EmailCodeException extends BaseException { public EmailCodeException(String msg) { super(msg); } }
注意:這個地方可以看我往期的文章,有寫全局異常處理器的使用方法
2.4、在Controller層編寫接口調(diào)用sendVerificationEmail接口參數(shù)為接收者郵箱
package com.sxy.recordnetwork.controller; import com.sxy.recordnetwork.DTO.USER.UserDtoEmailLogin; import com.sxy.recordnetwork.DTO.USER.UserDtoPhoneLogin; import com.sxy.recordnetwork.Exception.Son.EmailCodeException; import com.sxy.recordnetwork.Utils.EmailSendUtils; import com.sxy.recordnetwork.enumeration.Constants; import com.sxy.recordnetwork.common.Result; import com.sxy.recordnetwork.common.SendCode; import com.sxy.recordnetwork.service.UserService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.*; import java.util.concurrent.TimeUnit; /** * 第三方短信驗證以及其他相關(guān)操作 */ @RequestMapping("/code") @RestController @Slf4j public class CodeController { @Autowired private RedisTemplate redisTemplate; @Autowired private UserService userService; @Autowired private EmailSendUtils emailSendUtils; /** * 郵箱驗證碼發(fā)送 * @param receiveEmail * @return */ @GetMapping("/email/sendEmail") public Result sendSimpleMail(@RequestParam(value = "receiveEmail") String receiveEmail) { emailSendUtils.sendVerificationEmail(receiveEmail); return Result.success(Constants.EMAIL_SEND_SUCCESS.getValue(),null); } /** * qq郵箱驗證登錄 * @param emailLogin * @return */ @PostMapping("/emailLogin") public Result emailLogin(@RequestBody UserDtoEmailLogin emailLogin){ // 查看是否存在這一個qq郵箱 if(!userService.getEmail(emailLogin.getEmail())){ throw new EmailCodeException(Constants.NOT_EXIST_EMAIL.getValue()); } // 從redis數(shù)據(jù)庫中獲取email_code驗證碼用來做判斷 String email_code = (String) redisTemplate.opsForValue().get(Constants.EMAIL_CODE.getValue()); if(!emailLogin.getCode().equals(email_code)){ throw new EmailCodeException(Constants.EMAIL_CODE_ERROR.getValue()); } // 登陸成功 清楚本地redis郵箱驗證碼 redisTemplate.delete(Constants.EMAIL_CODE.getValue()); return Result.success(Constants.LOGIN.getValue(),userService.EmailLogin(emailLogin.getEmail())); } }
2.5、聲明的常量類:
因為項目中用到了大量的字符串,所以我這里就寫了常量類,來代替字符串
package com.sxy.recordnetwork.enumeration; import lombok.Getter; /** * 枚舉類,常量 */ @Getter public enum Constants { EMAIL_SEND_SUCCESS("皇上,臣妾已為您發(fā)送了郵箱哦!"),//郵件發(fā)送成功,請查收! EMAIL_MESSAGE("您的QQ郵箱驗證碼為:"), EMAIL_OUTTIME_TEN(",請在2分鐘內(nèi)完成驗證"), EMAIL_TITLE("QQ郵箱驗證碼"),// 郵箱驗證碼發(fā)送的標(biāo)題 EMAIL_CODE("email_code"),// 郵箱驗證碼redis的key EMAIL_CODE_ERROR("皇上,請核實您的QQ郵箱驗證碼是否正確!"),// qq郵箱驗證碼錯誤 NOT_EXIST_EMAIL("皇上,臣妾沒有找到您的QQ郵箱,請確認(rèn)是否正確!");// 郵箱不存在 private final String Value; Constants(String value){ this.Value = value; } }
好了,到這里就結(jié)束了,這樣的話一個qq郵箱驗證碼就通過發(fā)送了
總結(jié)
到此這篇關(guān)于springboot項目接入第三方qq郵箱驗證登錄的文章就介紹到這了,更多相關(guān)springboot接入qq郵箱驗證登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot實現(xiàn)郵箱驗證碼注冊與修改密碼及登錄功能詳解流程
- SpringBoot登錄驗證token攔截器的實現(xiàn)
- springboot整合shiro多驗證登錄功能的實現(xiàn)(賬號密碼登錄和使用手機(jī)驗證碼登錄)
- vue+springboot實現(xiàn)登錄驗證碼
- springboot短信驗證碼登錄功能的實現(xiàn)
- Springboot2.1.6集成activiti7出現(xiàn)登錄驗證的實現(xiàn)
- springboot實現(xiàn)郵箱驗證碼功能
- SpringBoot使用郵箱發(fā)送驗證碼實現(xiàn)注冊功能
- SpringBoot發(fā)送郵箱驗證碼功能
- SpringBoot如何使用mail實現(xiàn)登錄郵箱驗證
相關(guān)文章
Spring Boot多模塊化后,服務(wù)間調(diào)用的坑及解決
這篇文章主要介紹了Spring Boot多模塊化后,服務(wù)間調(diào)用的坑及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06java連接SQL?Server數(shù)據(jù)庫的超詳細(xì)教程
最近在java連接SQL數(shù)據(jù)庫時會出現(xiàn)一些問題,所以這篇文章主要給大家介紹了關(guān)于java連接SQL?Server數(shù)據(jù)庫的超詳細(xì)教程,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06SpringBoot 項目使用hutool 工具進(jìn)行 http 接口調(diào)用的處理方
在實際的開發(fā)過程中一個互聯(lián)網(wǎng)的項目來說 ,有可能會涉及到調(diào)用外部接口的實際業(yè)務(wù)場景,下面通過本文給大家介紹SpringBoot 項目 使用hutool 工具進(jìn)行 http 接口調(diào)用的處理方法,需要的朋友可以參考下2022-06-06關(guān)于synchronized、volatile、ReentrantLock的區(qū)別與對比
這篇文章主要介紹了關(guān)于synchronized、volatile、ReentrantLock的區(qū)別與對比,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04