Spring Boot實(shí)現(xiàn)qq郵箱驗(yàn)證碼注冊(cè)和登錄驗(yàn)證功能
1、登錄注冊(cè)思路
這是一個(gè)使用spring boot做的一個(gè)qq郵箱注冊(cè)和登錄的項(xiàng)目。
沒(méi)寫(xiě)前端頁(yè)面,使用postman測(cè)試。有截圖詳細(xì)。
1.1、思路
注冊(cè):通過(guò)輸入的郵箱發(fā)送驗(yàn)證碼,檢驗(yàn)前端傳來(lái)的驗(yàn)證碼是否和后臺(tái)生成的一致,若一致,將數(shù)據(jù)寫(xiě)入數(shù)據(jù)庫(kù),完成注冊(cè);
登錄:通過(guò)輸入的郵箱查詢密碼,然后比較密碼是否一致,一致就是登錄成功。
1.2、整個(gè)項(xiàng)目結(jié)構(gòu)圖
2、準(zhǔn)備
2.1、開(kāi)啟郵箱POP3/SMTP服務(wù)
登錄qq郵箱后,點(diǎn)擊左上方的設(shè)置,選擇賬戶,如下圖。
然后一直往下滑,看到如下圖的POP3/SMTP服務(wù),點(diǎn)擊開(kāi)啟,應(yīng)該會(huì)讓幫定的手機(jī)號(hào)發(fā)個(gè)短信,然后會(huì)收到一個(gè)授權(quán)碼,一定要好好保存,在appliction.properties配置中會(huì)用到。
2.2、創(chuàng)建一個(gè)spring boot項(xiàng)目的時(shí)候,一直確認(rèn),jdk選擇8。
下邊是pom.xml中<dependencies>標(biāo)簽的全部依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--jdbc--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> </dependencies>
2.3、application.properties配置文件
application.properties配置文件
#郵箱配置 #平臺(tái)地址,這里用的是qq郵箱,使用其他郵箱請(qǐng)更換 spring.mail.host = smtp.qq.com #改成自己的郵箱 spring.mail.username = xxxxx@qq.com #發(fā)送短信后它給你的授權(quán)碼 填寫(xiě)到這里 spring.mail.password = xxxxxx #這東西不用改 spring.mail.properties.mail.smtp.ssl.enable=true ##編碼格式 spring.mail.default-encoding=UTF-8 #數(shù)據(jù)庫(kù)相關(guān)配置 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/email?useSSL=true&characterEncoding=utf-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root #配置mapper mybatis.mapper-locations=classpath:mapper/*.xml
2.4、創(chuàng)建數(shù)據(jù)庫(kù)
數(shù)據(jù)庫(kù)結(jié)構(gòu)如下圖
創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)email
CREATE DATABASE email;
在email數(shù)據(jù)庫(kù)創(chuàng)建user表
CREATE TABLE `user` ( `id` int(20) NOT NULL AUTO_INCREMENT, `username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
3、全部代碼類
如最上邊的項(xiàng)目結(jié)構(gòu)圖。controller包是和前端對(duì)接的,mapper包中是接口,pojo是實(shí)體類,service層是邏輯代碼,vo包是前端發(fā)送數(shù)據(jù)暫時(shí)保存。
執(zhí)行流程: 使用postman發(fā)送請(qǐng)求,controller中會(huì)接受,然后調(diào)用service中的邏輯代碼,service會(huì)調(diào)用的mapper中接口,mapper的對(duì)應(yīng)的xml實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的各種操作。
3.1、UserController.java
package com.lu.youxiang.controller; import com.lu.youxiang.service.MailService; import com.lu.youxiang.vo.UserVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpSession; @Controller public class UserController { @Autowired private MailService mailService; @PostMapping("/sendEmail") @ResponseBody public String sendEmail(String email, HttpSession httpSession){ mailService.sendMimeMail(email, httpSession); return "sucess"; } @PostMapping("/regist") @ResponseBody public String regist(UserVo userVo, HttpSession session){ mailService.registered(userVo,session); return "sucess"; } @PostMapping("/login") @ResponseBody public String login(String email, String password){ mailService.loginIn(email,password); return "sucess"; } }
3.2、UserMapper.java
package com.lu.youxiang.mapper; import com.lu.youxiang.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; @Mapper @Repository public interface UserMapper { /** * 注冊(cè),插入數(shù)據(jù) * @param user */ void insertUser(User user); /** * 根據(jù)郵箱查詢 * @param email * @return */ User queryByEmail(String email); }
3.3、User.java
package com.lu.youxiang.pojo; public class User { private String username; private String password; private String email; //get和set方法省略了,自己生成一下 }
3.4、MailService.java ,重要。
package com.lu.youxiang.service; import com.lu.youxiang.mapper.UserMapper; import com.lu.youxiang.pojo.User; import com.lu.youxiang.vo.UserVo; import com.lu.youxiang.vo.UserVoToUser; 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 javax.servlet.http.HttpSession; import java.util.Random; @Service public class MailService { @Autowired private JavaMailSender mailSender;//一定要用@Autowired @Autowired private UserMapper userMapper;//注入U(xiǎn)serMapper,交給bena //application.properties中已配置的值 @Value("${spring.mail.username}") private String from; /** * 給前端輸入的郵箱,發(fā)送驗(yàn)證碼 * @param email * @param session * @return */ public boolean sendMimeMail( String email, HttpSession session) { try { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setSubject("驗(yàn)證碼郵件");//主題 //生成隨機(jī)數(shù) String code = randomCode(); //將隨機(jī)數(shù)放置到session中 session.setAttribute("email",email); session.setAttribute("code",code); mailMessage.setText("您收到的驗(yàn)證碼是:"+code);//內(nèi)容 mailMessage.setTo(email);//發(fā)給誰(shuí) mailMessage.setFrom(from);//你自己的郵箱 mailSender.send(mailMessage);//發(fā)送 return true; }catch (Exception e){ e.printStackTrace(); return false; } } /** * 隨機(jī)生成6位數(shù)的驗(yàn)證碼 * @return String code */ public String randomCode(){ StringBuilder str = new StringBuilder(); Random random = new Random(); for (int i = 0; i < 6; i++) { str.append(random.nextInt(10)); } return str.toString(); } /** * 檢驗(yàn)驗(yàn)證碼是否一致 * @param userVo * @param session * @return */ public boolean registered(UserVo userVo, HttpSession session){ //獲取session中的驗(yàn)證信息 String email = (String) session.getAttribute("email"); String code = (String) session.getAttribute("code"); //獲取表單中的提交的驗(yàn)證信息 String voCode = userVo.getCode(); //如果email數(shù)據(jù)為空,或者不一致,注冊(cè)失敗 if (email == null || email.isEmpty()){ //return "error,請(qǐng)重新注冊(cè)"; return false; }else if (!code.equals(voCode)){ //return "error,請(qǐng)重新注冊(cè)"; return false; } //保存數(shù)據(jù) User user = UserVoToUser.toUser(userVo); //將數(shù)據(jù)寫(xiě)入數(shù)據(jù)庫(kù) userMapper.insertUser(user); //跳轉(zhuǎn)成功頁(yè)面 return true; } /** * 通過(guò)輸入email查詢password,然后比較兩個(gè)password,如果一樣,登錄成功 * @param email * @param password * @return */ public boolean loginIn(String email, String password){ User user = userMapper.queryByEmail(email); if(!user.getPassword().equals(password)){ return false; } System.out.println("登錄成功:數(shù)據(jù)庫(kù)密碼是:"+user.getPassword()); return true; } }
3.5、UserVo.java
package com.lu.youxiang.vo; public class UserVo { private String username; private String password; private String email; // 驗(yàn)證碼 private String code; //省略了get和set方法,自己生成一下 }
3.6、UserVoToUser.java
package com.lu.youxiang.vo; import com.lu.youxiang.pojo.User; public class UserVoToUser { /** * 將表單中的對(duì)象轉(zhuǎn)化為數(shù)據(jù)庫(kù)中存儲(chǔ)的用戶對(duì)象(剔除表單中的code) * @param userVo * @return */ public static User toUser(UserVo userVo) { //創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)中存儲(chǔ)的對(duì)象 User user = new User(); //傳值 user.setUsername(userVo.getUsername()); user.setPassword(userVo.getPassword()); user.setEmail(userVo.getEmail()); // 返回包裝后的對(duì)象 return user; } }
主配置類不復(fù)制了,創(chuàng)建springboot項(xiàng)目后,就有。
3.7、UserMapper.xml
在resources包下創(chuàng)建mapper包,用來(lái)放xml,然后再這個(gè)包中創(chuàng)建UserMapper.xml,內(nèi)容如下。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.lu.youxiang.mapper.UserMapper"> <insert id="insertUser" parameterType="com.lu.youxiang.pojo.User"> insert into user (username,password,email) values (#{username},#{password},#{email}) </insert> <select id="queryByEmail" resultType="com.lu.youxiang.pojo.User"> select * from user where email = #{email} </select> </mapper>
4、使用postman測(cè)試
如果沒(méi)有這個(gè)軟件,安裝一下,使用很簡(jiǎn)單。
打開(kāi)后,點(diǎn)擊左上角的file,再點(diǎn)擊New Tab,就會(huì)出來(lái)一個(gè)頁(yè)面。
(或者使用Ctrl+T
快捷鍵)
4.1、測(cè)試發(fā)送郵件
請(qǐng)求url:
http://localhost:8080/sendEmail?email=123456@qq.com
把請(qǐng)求url復(fù)制到如下如的url中, 郵箱換成自己的,請(qǐng)求方式換成POST。點(diǎn)擊send。
如下圖
4.2、測(cè)試注冊(cè)
請(qǐng)求url:
http://localhost:8080/regist
把請(qǐng)求url復(fù)制到如下如的url中, 郵箱換成自己的,code的值寫(xiě)郵箱收到的,請(qǐng)求方式換成POST。點(diǎn)擊send,如下圖
4.3、測(cè)試登錄
請(qǐng)求url:
http://localhost:8080/login?email=123456@qq.com&password=12345
復(fù)制url,改成POST請(qǐng)求,點(diǎn)擊Send。
如下圖
總結(jié)
到此這篇關(guān)于Spring Boot實(shí)現(xiàn)qq郵箱驗(yàn)證碼注冊(cè)和登錄驗(yàn)證功能的文章就介紹到這了,更多相關(guān)SpringBoot qq郵箱驗(yàn)證碼注冊(cè)和登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用Postman和Chrome的開(kāi)發(fā)者功能探究項(xiàng)目(畢業(yè)設(shè)計(jì)項(xiàng)目)
這篇文章主要介紹了利用Postman和Chrome的開(kāi)發(fā)者功能探究項(xiàng)目(畢業(yè)設(shè)計(jì)項(xiàng)目),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Flink實(shí)現(xiàn)特定統(tǒng)計(jì)的歸約聚合reduce操作
這篇文章主要介紹了Flink實(shí)現(xiàn)特定統(tǒng)計(jì)的歸約聚合reduce操作,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02IntelliJ IDEA查看方法說(shuō)明文檔的圖解
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA查看方法說(shuō)明文檔的圖解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10Spring中的底層架構(gòu)核心概念類型轉(zhuǎn)換器詳解
這篇文章主要介紹了Spring中的底層架構(gòu)核心概念類型轉(zhuǎn)換器詳解,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12SpringBoot中@ConfigurationProperties注解實(shí)現(xiàn)配置綁定的三種方法
這篇文章主要介紹了SpringBoot中@ConfigurationProperties注解實(shí)現(xiàn)配置綁定的三種方法,文章內(nèi)容介紹詳細(xì)需要的小伙伴可以參考一下2022-04-04Java安全框架——Shiro的使用詳解(附springboot整合Shiro的demo)
這篇文章主要介紹了Java安全框架——Shiro的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用Shiro,感興趣的朋友可以了解下2021-04-04Java實(shí)現(xiàn)將Word轉(zhuǎn)換成Html的示例代碼
在業(yè)務(wù)中,常常會(huì)需要在瀏覽器中預(yù)覽Word文檔,或者需要將Word文檔轉(zhuǎn)成HTML文件保存,本文主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)Word轉(zhuǎn)換成Html的相關(guān)方法,希望對(duì)大家有所幫助2024-02-02RocketMQ NameServer保障數(shù)據(jù)一致性實(shí)現(xiàn)方法講解
這篇文章主要介紹了RocketMQ NameServer保障數(shù)據(jù)一致性實(shí)現(xiàn)方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12