SpringBoot實(shí)現(xiàn)短信驗(yàn)證碼登錄功能(案例)
一、要找一個(gè)提供短信接口的第三方平臺(tái),這里我使用的是榛子云
二、在注冊(cè)后,就可以使用了
三、首先是在pom.xml中添加依賴
<!-- fastjosn --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>com.zhenzikj</groupId> <artifactId>zhenzisms</artifactId> <version>1.0.2</version> </dependency>
1.驗(yàn)證碼發(fā)送的controller
package com.foreknow.controller; import com.alibaba.fastjson.JSONObject; import com.foreknow.model.Member; import com.foreknow.service.MemberService; import com.zhenzi.sms.ZhenziSmsClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpSession; import java.util.Random; @Controller public class CodeController { //短信平臺(tái)相關(guān)參數(shù) //這個(gè)不用改 private String apiUrl = "https://sms_developer.zhenzikj.com"; //榛子云系統(tǒng)上獲取 private String appId = "100862"; private String appSecret = "62358d10-bc0e-4152-a52c-578a8debc9b9"; @ResponseBody @GetMapping("/fitness/code") public boolean getCode(@RequestParam("memPhone") String memPhone, HttpSession httpSession){ try { JSONObject json = null; //隨機(jī)生成驗(yàn)證碼 String code = String.valueOf(new Random().nextInt(999999)); //將驗(yàn)證碼通過榛子云接口發(fā)送至手機(jī) ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret); String result = client.send(memPhone, "您的驗(yàn)證碼為:" + code + ",該碼有效期為5分鐘,該碼只能使用一次!"); json = JSONObject.parseObject(result); if (json.getIntValue("code")!=0){//發(fā)送短信失敗 return false; } //將驗(yàn)證碼存到session中,同時(shí)存入創(chuàng)建時(shí)間 //以json存放,這里使用的是阿里的fastjson json = new JSONObject(); json.put("memPhone",memPhone); json.put("code",code); json.put("createTime",System.currentTimeMillis()); // 將認(rèn)證碼存入SESSION httpSession.setAttribute("code",json); return true; } catch (Exception e) { e.printStackTrace(); return false; } } }
其中的局部變量是在榛子云的個(gè)人中心獲?。?/p>
登錄時(shí)從session中獲取剛剛發(fā)送到手機(jī)的驗(yàn)證碼對(duì)象:
JSONObject userCode = (JSONObject)session.getAttribute("code"); //驗(yàn)證碼 userCode .get("code"); //手機(jī)號(hào) userCode.get("memPhone");
前端限制60秒只能獲取一次驗(yàn)證碼的效果實(shí)現(xiàn):
<div id="model2"> <div class="layui-form-item input-item"> <label for="userName">手機(jī)號(hào)</label> <input type="text" placeholder="請(qǐng)輸入手機(jī)號(hào)" autocomplete="off" id="memPhone" name="memPhone" class="layui-input"> </div> <div class="layui-form-item input-item"> <label for="userName">驗(yàn)證碼</label> <input type="text" placeholder="請(qǐng)輸入驗(yàn)證碼" autocomplete="off" id="code" name="code" maxlength="6" class="layui-input" style="width: 50%;display: inline"> <input type="button" class="layui-btn layui-btn-primary" value="獲取驗(yàn)證碼" id="sendBtn" style="width:41%;margin-left: 18px;border-color:#1e9fff !important;" onclick="sendCode(this)"></input> </div> </div> function sendCode(){ var memPhone = $("#memPhone").val(); console.log(memPhone.length); if(memPhone == '' || memPhone.length != 11){ layer.msg("請(qǐng)輸入正確的手機(jī)號(hào)!"); return; }else{ $.ajax({ type: 'GET', url: '[[${basePath}]]/fitness/code', data: { memPhone : memPhone }, dataType: 'json', success: function(data) { if(data){ timer(); }else{ layer.msg("獲取驗(yàn)證碼失敗"); } }, error: function(data) { layer.msg('連接超時(shí)!'); }, }); } } var wait = 60; //倒計(jì)時(shí) function timer() { if(wait == 0){ $("#sendBtn").val("獲取驗(yàn)證碼"); $("#sendBtn").removeAttr("disabled"); $("#sendBtn").css("border-color","1e9fff").css("background", "#ffffff").css("cursor", "pointer"); wait = 60; }else{ $("#sendBtn").attr("disabled","true"); $("#sendBtn").css("border-color","fbfbfb").css("background", "#ccc").css("cursor", "not-allowed"); $("#sendBtn").val(wait + "秒后重發(fā)"); wait--; setTimeout(function() {timer()}, 1000); } }
到此這篇關(guān)于SpringBoot實(shí)現(xiàn)短信驗(yàn)證碼登錄的文章就介紹到這了,更多相關(guān)SpringBoot短信驗(yàn)證碼登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android字體相關(guān)知識(shí)總結(jié)
最近接到一個(gè)需求,大致內(nèi)容是:全局替換當(dāng)前項(xiàng)目中的默認(rèn)字體,并引入 UI 設(shè)計(jì)師提供的一些新字體。于是對(duì)字體做了些研究,把自己的一些心得分享給大家。注意:本文所展示的系統(tǒng)源碼都是基于Android-30 ,并提取核心部分進(jìn)行分析2021-06-06Android仿打開微信紅包動(dòng)畫效果實(shí)現(xiàn)代碼
這篇文章主要介紹了Android仿打開微信紅包動(dòng)畫效果實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12在Android模擬器上模擬GPS功能總是null的解決方法
在我們開發(fā)時(shí)需要在模擬器上模擬GPS,可在Location的時(shí)候總是null,下面與大家分享下具體的解決方法,感興趣的朋友可以參考下哈2013-06-06Android實(shí)現(xiàn)倒計(jì)時(shí)的方案梳理
這篇文章主要介紹了Android實(shí)現(xiàn)倒計(jì)時(shí)的方案梳理,下面文章圍繞主題展開Android倒計(jì)時(shí)方案,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08Android RecyclerView滑動(dòng)刪除和拖動(dòng)排序
這篇文章主要介紹了Android RecyclerView滑動(dòng)刪除和拖動(dòng)排序的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07詳解Android?Flutter如何自定義動(dòng)畫路由
flutter中有默認(rèn)的Route組件,叫做MaterialPageRoute,但是MaterialPageRoute太普通了,如果我們想要做點(diǎn)不同的跳轉(zhuǎn)特效應(yīng)該如何處理呢?一起來看看吧2023-04-04Android xml實(shí)現(xiàn)animation的4種動(dòng)畫效果實(shí)例代碼
在Android應(yīng)用程序,使用動(dòng)畫效果,能帶給用戶更好的感覺,做動(dòng)畫可以通過XML或Android代碼來實(shí)現(xiàn)。本文給大家介紹Android xml實(shí)現(xiàn)animation的4種動(dòng)畫效果實(shí)例代碼,一起看看吧2016-05-05android實(shí)現(xiàn)通知欄下載更新app示例
這篇文章主要介紹了android實(shí)現(xiàn)通知欄下載更新app示例,需要的朋友可以參考下2014-03-03