欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot與vue詳解實現(xiàn)短信發(fā)送流程

 更新時間:2022年06月06日 11:41:28   作者:仲夏月二十八  
隨著人工智能的不斷發(fā)展,機器學(xué)習(xí)這門技術(shù)也越來越重要,很多人都開啟了學(xué)習(xí)機器學(xué)習(xí),本文就介紹了機器學(xué)習(xí)的基礎(chǔ)內(nèi)容

一、前期工作

1.開啟郵箱服務(wù)

開啟郵箱的POP3/SMTP服務(wù)(這里以qq郵箱為例,網(wǎng)易等都是一樣的)

2.導(dǎo)入依賴

在springboot項目中導(dǎo)入以下依賴:

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.3.18</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.17</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

3.配置application.yaml文件

# 郵箱設(shè)置
spring:
 mail:
   host: smtp.163.com //如果是qq的郵箱就是smtp.qq.com
   password: 開啟pop3生成的一個字符串密碼
   username: 自己的郵箱地址,是什么郵箱后綴就是什么。
   port:
   default-encoding: UTF-8
   protocol: smtp
   properties:
     mail.smtp.auth: true
     mail.smtp.starttls.enable: true
     mail.smtp.starttls.required: true
     mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactory
     mail.smtp.socketFactory.fallback: false
     mail:
       smtp:
         ssl:
           enable: true
 mvc:
   pathmatch:
     matching-strategy: ant_path_matcher
 datasource: # jdbc數(shù)據(jù)庫設(shè)置
   druid:
     password: sql密碼
     username: sql用戶
     url: jdbc:mysql://localhost:3306/sys?charsetEncoding=utf-8&useSSL=false
     driver-class-name: com.mysql.jdbc.Driver
     db-type: com.alibaba.druid.pool.DruidDataSource
mybatis: #mybatis的配置
 type-aliases-package: com.cheng.springcolud.pojo
 config-location: classpath:mybatis/mybatis-config.xml
 mapper-locations: classpath:mybatis/mapper/*.xml

二、實現(xiàn)流程

1.導(dǎo)入數(shù)據(jù)庫

/*
 Navicat Premium Data Transfer
 Source Server         : likai
 Source Server Type    : MySQL
 Source Server Version : 50719
 Source Host           : localhost:3306
 Source Schema         : sys
 Target Server Type    : MySQL
 Target Server Version : 50719
 File Encoding         : 65001
 Date: 04/06/2022 14:08:29
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for useremaillogintable
-- ----------------------------
DROP TABLE IF EXISTS `useremaillogintable`;
CREATE TABLE `useremaillogintable`  (
  `id` int(255) NOT NULL AUTO_INCREMENT,
  `email` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `VerificationCode` int(20) NULL DEFAULT NULL,
  `createTime` datetime(0) NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP(0),
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;

2.后端實現(xiàn)

編寫實體類

代碼如下(示例):

@Data
@NoArgsConstructor
@ToString
public class EmailVerification {
    private int id;
    private String email; //需要發(fā)送的郵箱
    private Integer verificationCode;//驗證碼
    private Date createTime;
    public EmailVerification(String email, Integer verificationCode) {
        this.email = email;
        this.verificationCode = verificationCode;
    }
}

編寫工具類ResultVo

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultVO {
    private int code;//相應(yīng)給前端的狀態(tài)碼
    private String msg;//相應(yīng)給前端的提示信息
    private Object data;//響應(yīng)給前端的數(shù)據(jù)
}

編寫dao層接口

Mapper
@Repository
public interface EmailVerificationDao {
    /*將短信驗證碼和個人郵箱保存到數(shù)據(jù)庫中*/
    public Boolean getEmailVerificationCode(String email,Integer verificationCode);
    /*校驗短信信息*/
    public List<EmailVerification> checkEmailVerificationCode(String email,Integer verificationCode);
    /*查詢所有的用戶*/
    public List<EmailVerification> queryEmailVerificationInfo();
}

配置dao層接口的數(shù)據(jù)庫操作

<?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.cheng.dao.EmailVerificationDao">
    <insert id="getEmailVerificationCode">
        insert into sys.useremaillogintable(email, VerificationCode,createTime) VALUES (#{email},#{verificationCode},NOW())
    </insert>
    <select id="checkEmailVerificationCode" resultType="com.cheng.bean.EmailVerification">
        select * from sys.useremaillogintable where email=#{email} and VerificationCode=#{verificationCode}
    </select>
    <select id="queryEmailVerificationInfo" resultType="com.cheng.bean.EmailVerification" >
        select * from sys.useremaillogintable;
    </select>
</mapper>

編寫service層接口

public interface EmailVerificationCodeService {
    public boolean getEmailVerificationCode(String email,Integer verificationCode);
    public ResultVO checkEmailVerificationCode(String email, Integer verificationCode);
    public ResultVO queryEmailVerificationInfo();
    public ResultVO sendEmailVerificationCode(String email);
}

代碼講解: getEmailVerificationCod方法是將數(shù)據(jù)(驗證碼和郵箱地址)放入數(shù)據(jù)庫當(dāng)中,checkEmailVerificationCode是用來校驗其驗證碼和郵箱是否是正確,·queryEmailVerificationInfo·是用來查詢所有的數(shù)據(jù),在這里我新加了個接口叫做senEmailVerificationCode就是單純用來發(fā)送短信信息的,只有一個參數(shù),他是沒有相對應(yīng)的數(shù)據(jù)庫操作的。

編寫service層的實現(xiàn)方法

@Service
public class EmailVerificationCodeServiceImpl implements EmailVerificationCodeService{
    @Autowired
    EmailVerificationDao emailVerificationDao;
    private final static String EmailFrom = "li3122456997@163.com";
    private final JavaMailSenderImpl javaMailSender;
    public int code = (int)(Math.random() * 9000 + 1000);
    public EmailVerificationCodeServiceImpl(JavaMailSenderImpl javaMailSender) {
        this.javaMailSender = javaMailSender;
    }
    @Override
    public boolean getEmailVerificationCode(String email,Integer verificationCode) {
            verificationCode =code;
            return emailVerificationDao.getEmailVerificationCode(email,verificationCode);
    }
    @Override
    public ResultVO checkEmailVerificationCode(String email, Integer verificationCode) {
        List<EmailVerification> emailVerifications = emailVerificationDao.checkEmailVerificationCode(email, verificationCode);
        if (emailVerifications.size()>0){
            return new ResultVO(1001,"校驗成功",emailVerifications);
        }else {
            return new ResultVO(1002,"校驗失敗",null);
        }
    }
    @Override
    public ResultVO queryEmailVerificationInfo() {
        List<EmailVerification> emailVerifications = emailVerificationDao.queryEmailVerificationInfo();
        return new ResultVO(1001,"success",emailVerifications);
    }
    @Override
    public ResultVO sendEmailVerificationCode(String email) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("驗證碼");
        simpleMailMessage.setTo(email);//收件人
        simpleMailMessage.setText("驗證碼為:"+code);
        simpleMailMessage.setFrom("******@163.com"); //發(fā)送的人(寫自己的)
        javaMailSender.send(simpleMailMessage);
        boolean emailVerificationCode = getEmailVerificationCode(email, code);
        if (emailVerificationCode){
            return new ResultVO(1001,"發(fā)送成功!","驗證碼為:"+code);
        }else {
            return new ResultVO(1002,"發(fā)送失敗",null);
        }
    }
}

代碼講解: 這里就一個注重點,就是sendEmailVerificationCode的實現(xiàn),我將隨機數(shù)給提出出來,因為getEmailVerificationCode也是需要將隨機數(shù)給保存到數(shù)據(jù)庫當(dāng)中的,為了避免兩者的驗證碼不同,我就給其提取出來,以確保其一致性,在sendEmailVerificationCode的實現(xiàn),我在里面調(diào)用了getEmailVerificationCode方法,這樣可以保證其郵箱地址的一致性。在通過判斷,驗證短信是否發(fā)送成功。

實現(xiàn)controller層

@RestController
@CrossOrigin//允許回復(fù)前端數(shù)據(jù),跨域請求允許
public class EmailController {
    @Autowired
    EmailVerificationCodeService emailVerificationCodeService;
    @Autowired
    InfoTimingSendServiceImpl infoTimingSendService;
    @GetMapping("send")
    public ResultVO sendMail(@RequestParam(value = "email") String email){
        return emailVerificationCodeService.sendEmailVerificationCode(email);
    }
    @GetMapping("checkEmailVerification")
    public ResultVO checkEmail(@RequestParam(value = "email") String email, @RequestParam(value = "verificationCode") Integer verificationCode){
        ResultVO resultVO = emailVerificationCodeService.checkEmailVerificationCode(email, verificationCode);
        return resultVO;
    }
    @GetMapping("queryAll")
    public ResultVO queryAll(){
        ResultVO resultVO = emailVerificationCodeService.queryEmailVerificationInfo();
        return resultVO;
    }
}

注意: 需要加入@CrossOrigin注解,這個注解是可以解決跨域問題,這個項目我寫的是前后端分離的,所以這里需要加入這個在注解,為后面通過axios來獲取數(shù)據(jù)做準(zhǔn)備

Test代碼

@SpringBootTest
class DemoApplicationTests {
    @Autowired
    EmailVerificationCodeService emailVerificationCodeService;
    @Autowired
    InfoTimingSendServiceImpl infoTimingSendService;
    @Test
    void contextLoads() {
        ResultVO aBoolean = emailVerificationCodeService.checkEmailVerificationCode("***@qq.com", 8001);
        System.out.println(aBoolean);
    }
    @Test
    void infoSendTest(){
        infoTimingSendService.infoSend();
    }
    @Test
    void send(){
        final ResultVO resultVO = emailVerificationCodeService.sendEmailVerificationCode("***7@qq.com");
        System.out.println(resultVO);
    }
}

前端頁面的實現(xiàn)

注意: 在前端頁面我使用了bootstrap框架,vue,axios,所以需要當(dāng)如相對應(yīng)的包

注冊頁面

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<link rel="stylesheet" href="css/bootstrap.min.css" rel="external nofollow"  />	
		<script src="js/jquery.min.js"></script>
		<script src="js/bootstrap.min.js"></script>
		<script src="js/vue.js"></script>
		<style>
		</style>
		</head>
	<body style="background: linear-gradient(to right,#7b4397,#dc2430);">
		<div id="container">
		<div class="container-fluid" style="color: white;">
		  <form class="form-horizontal" role="form" style="padding-left: 500px;  margin-top: 200px;">
				<fieldset>
		    <div class="form-group">
						<p style="font-size: 30px;margin-left: 250px; margin-right: 240px;color:white">登錄</p>
						<hr style="width: 400px;margin-left: 90px; background-color: red;" />
						<p style="font-size: 15px;margin-left: 190px; margin-right: 240px;color:red" id="tips">{{tips}} &nbsp;</p>
		      <label for="inputEmail3" class="col-sm-2 control-label">郵箱</label>
		      <div class="col-sm-3">
		        <input type="email" v-model="email" class="form-control" id="inputEmail3" placeholder="Email">
		      </div>
		    </div>
		    <div class="form-group">
		      <label for="inputPassword3" class="col-sm-2 control-label">驗證碼</label>
		      <div class="col-sm-3">
		        <input v-if="verification_send" type="text" v-model="verification" class="form-control" id="inputPassword3" placeholder="VerificationCode">
						<input v-else type="text"  class="form-control" v-model="verification" id="inputPassword3" placeholder="5分鐘內(nèi)有效,60s后可重新發(fā)送" />
		      </div>
		    </div>
		    <div class="form-group">
		      <div class="col-sm-offset-2 col-sm-3">
		        <a type="submit" class="btn btn-default" @click="sign">Sign in</a>
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<a v-if="time==60" class="btn btn-default" @click="send" class="btn btn-default">{{info}}</a>
						<a v-if="time<60&&time>=0"  class="btn btn-default" @click="send" class="btn btn-default" style="pointer-events: none;">{{"wait"+time+"s"}}</a>
						<a v-if="time<0" class="btn btn-default" @click="send" class="btn btn-default">{{info}}</a>
		      </div>
		    </div>
		  </form>
		</div>
		</div>
		<script src="js/axios.min.js"></script>
		<script type="text/javascript">
			var baseUrl="http://localhost:8080/";
			var vm = new Vue({
				el:'#container',
				data:{
					info: 'send out',
					time: 60,
					verification_send: true,
					verification:"",
					email:"",
					tips:""
				},
				methods:{
					send:function (){
						var url1 = baseUrl+"/send";
						axios.get(url1,{params:{email:vm.email}}).then((res)=>{
							console.log(res);
							if(res.data.code==1001){
								vm.tips="發(fā)送成功!";
							}else{
								vm.tips="發(fā)送失?。≌埳院笤僭?
							}
						});
						setInterval(function(){
							if(vm.time==60){
								vm.time--;
								this.time=vm.time;
								vm.verification_send = false;
								console.log(vm.time);
							}else if(vm.time==0){
								clearInterval(vm.time)
								vm.verification_send = true;
							}else{
								vm.time--;
								console.log(vm.time);
							}
						},1000);
					},
					sign:function(){
						var url = baseUrl+"/checkEmailVerification";
						if(vm.email==""&&vm.verification==""){
							vm.tips="請輸入驗證碼或郵箱!";
						}else{
							axios.get(url,{params:{email:vm.email,verificationCode:vm.verification}})
							.then((res)=>{
								var vo = res.data;
								if(vo.code==1001){
									vm.tips="登錄成功";
									setInterval(function(){
										window.location.href="index.html" rel="external nofollow" ;
									},3000);
								}else{
									vm.tips="請輸入正確的驗證碼!";
								}
							})
						}
					}
					}
			})
		</script>
	</body>
</html>

講解:在這里,在發(fā)送按鈕上面加入了時間倒計時,當(dāng)我點擊的時候,會倒計時1minute,在這期間,發(fā)送按鈕是無法被點擊的!這就避免了多次放松

index.htm

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<center style="margin-top: 50px;">
			<p>歡迎你:</p>
			<p>登錄成功!</p>
		</center>
	</body>
</html>

頁面效果:

效果圖:

運行截圖+sql圖

總結(jié)

以上就是springboot+vue實現(xiàn)后端和前端短信發(fā)送的所有代碼,其實像短信發(fā)送了兩次,以第二次為準(zhǔn)的話,我們可以實現(xiàn)一個數(shù)據(jù)庫內(nèi)容的修改,當(dāng)其發(fā)送了兩次,我們就以第二次為準(zhǔn)!希望對大家有所幫助,這里前端的驗證其實是不夠完善的,我沒有去加入郵箱的驗證。是因為我的QQ郵箱被騰訊被封了,我害怕試多了之后,網(wǎng)易郵箱也被封了?。。?!????

配張QQ郵箱被封的截圖鎮(zhèn)樓

到此這篇關(guān)于springboot與vue詳解實現(xiàn)短信發(fā)送流程的文章就介紹到這了,更多相關(guān)springboot短信發(fā)送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot無法訪問/static下靜態(tài)資源的解決

    SpringBoot無法訪問/static下靜態(tài)資源的解決

    這篇文章主要介紹了SpringBoot無法訪問/static下靜態(tài)資源的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • mac 安裝java1.8的過程詳解

    mac 安裝java1.8的過程詳解

    這篇文章主要介紹了mac 安裝java1.8,包括下載過程及配置環(huán)境相關(guān)知識介紹,本文結(jié)合實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • SpringBoot整合aop面向切面編程過程解析

    SpringBoot整合aop面向切面編程過程解析

    這篇文章主要介紹了SpringBoot整合aop面向切面編程過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • SpringBoot整合canal實現(xiàn)數(shù)據(jù)緩存一致性解決方案

    SpringBoot整合canal實現(xiàn)數(shù)據(jù)緩存一致性解決方案

    canal主要用途是基于?MySQL?數(shù)據(jù)庫增量日志解析,提供增量數(shù)據(jù)訂閱和消費,canal是借助于MySQL主從復(fù)制原理實現(xiàn),本文將給大家介紹SpringBoot整合canal實現(xiàn)數(shù)據(jù)緩存一致性解決方案,需要的朋友可以參考下
    2024-03-03
  • IDEA打包普通web項目操作

    IDEA打包普通web項目操作

    這篇文章主要介紹了IDEA打包普通web項目操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • java實現(xiàn)策略模式使用示例

    java實現(xiàn)策略模式使用示例

    在使用圖像處理軟件處理圖片后,需要選擇一種格式進(jìn)行保存。然而各種格式在底層實現(xiàn)的算法并不相同,這剛好適合策略模式。編寫程序,演示如何使用策略模式與簡單工廠模式組合進(jìn)行開發(fā)
    2014-02-02
  • Java 內(nèi)部類的定義與范例

    Java 內(nèi)部類的定義與范例

    說起內(nèi)部類這個詞,想必很多人都不陌生,但是又會覺得不熟悉。原因是平時編寫代碼時可能用到的場景不多,用得最多的是在有事件監(jiān)聽的情況下,并且即使用到也很少去總結(jié)內(nèi)部類的用法。今天我們就來一探究竟
    2021-11-11
  • springboot中如何將logback切換為log4j2

    springboot中如何將logback切換為log4j2

    springboot默認(rèn)使用logback作為日志記錄框架,常見的日志記錄框架有l(wèi)og4j、logback、log4j2,這篇文章我們來學(xué)習(xí)怎樣將logbak替換為log4j2,需要的朋友可以參考下
    2023-06-06
  • spring框架集成flyway項目的詳細(xì)過程

    spring框架集成flyway項目的詳細(xì)過程

    今天通過本文給大家分享spring框架集成flyway項目的詳細(xì)過程,由于大多數(shù)都是springboot集成flyway,很少見到spring框架的項目,今天就抽空給大家介紹下spring框架集成flyway項目的方法,一起看看吧
    2021-07-07
  • Spring的初始化前中后詳細(xì)解讀

    Spring的初始化前中后詳細(xì)解讀

    這篇文章主要介紹了Spring的初始化前中后詳細(xì)解讀,Spring?框架是一個非常流行的?Java?框架,它提供了一種輕量級的、可擴展的方式來構(gòu)建企業(yè)級應(yīng)用程序,在?Spring?的生命周期中,有三個重要的階段,即初始化前、初始化、初始化后,需要的朋友可以參考下
    2023-09-09

最新評論