SpringBoot使用榛子云實(shí)現(xiàn)手機(jī)短信發(fā)送驗(yàn)證碼
首先去榛子云官方平臺(tái)申請(qǐng)注冊(cè)自己的賬號(hào),官方網(wǎng)站:http://smsow.zhenzikj.com/
有賬號(hào)的話就直接登錄,沒有注冊(cè)一個(gè)即可,很簡(jiǎn)單的注冊(cè)
登錄成功后就是這個(gè)樣子,官方提供免費(fèi)發(fā)送一條,但是我反復(fù)測(cè)試一些功能效果顯然1條是不夠的,我沖了20,為了開發(fā)我沖了!??!憑這20元,我要20個(gè)贊不過分吧QAQ, 充值最低的話是20元,支持微信支付寶支付,一條短信也就3分錢左右,可以給朋友裝b用什么的,接下來進(jìn)入正題 在"應(yīng)用管理"——>"我的應(yīng)用"里,AppId,AppSecret,這都是用戶的唯一標(biāo)識(shí),很重要,一會(huì)在Java代碼中要用到
在"短信管理"——>"短信模板"中可以看到自己要發(fā)送的短信模板,會(huì)有自己默認(rèn)的模板,當(dāng)然也可以自己新建自定義模板,不要違規(guī)就好,這邊需要注意的是審核狀態(tài),審核通過后可以發(fā)送短信,我遇到好幾次出錯(cuò)都是因?yàn)槲倚陆ǖ哪0暹€沒審核通過就發(fā)送,所以一直失敗,這也是我后面想到的
做到這里,以及可以實(shí)現(xiàn)簡(jiǎn)單的借助第三方發(fā)送短信啦,但是逼格肯定不夠,進(jìn)別人的網(wǎng)址操作肯定不是我們的最終目標(biāo),我們接下來把它搬到自己的項(xiàng)目代碼中進(jìn)行實(shí)現(xiàn),這邊我以SpringBoot為例
話不多說直接上代碼
1.pom添加依賴
<!--轉(zhuǎn)換json數(shù)據(jù)--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.58</version> </dependency> <!--榛子云提供短信接口平臺(tái)--> <dependency> <groupId>com.zhenzikj</groupId> <artifactId>zhenzisms</artifactId> <version>2.0.2</version> </dependency>
2.創(chuàng)建controller方法寫發(fā)送方法
package com.wyh.controller; import com.alibaba.fastjson.JSONObject; import com.zhenzi.sms.ZhenziSmsClient; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpSession; import java.util.HashMap; import java.util.Map; import java.util.Random; /** * @program: SpringBoot_01 * @description: 短信發(fā)送 * @author: wyh * @createDate: 2021-04-27 22:24 **/ @Controller public class SendCodeController { //短信平臺(tái)相關(guān)參數(shù) //這個(gè)不用改 private String apiUrl = "https://sms_developer.zhenzikj.com"; //榛子云系統(tǒng)上獲取 private String appId = "108850"; private String appSecret = "NzhmN2JhNGQtNmRmOC00MWIwLTk1OGEtOTEyYzFiYjFlY2Vk"; @ResponseBody @RequestMapping("/sendCode") public boolean getCode(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); Map<String, Object> params = new HashMap<String, Object>(); //前臺(tái)輸入的手機(jī)號(hào) params.put("number", memPhone); //這個(gè)模板id對(duì)應(yīng)的是榛子云個(gè)人中心的模板id params.put("templateId", 5032); String[] templateParams = new String[2]; templateParams[0] = code; templateParams[1] = "5分鐘"; params.put("templateParams", templateParams); String result = client.send(params); System.out.println(result); 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; } } /** /* @Author wyh * @Description 跳轉(zhuǎn)發(fā)送短信頁面 * @Date 21:01 2021/5/7 * @Param [] * @return java.lang.String **/ @RequestMapping("/goSendCode") public String goSendCode(){ return "/sendCode"; } }
3.新建短信發(fā)送jsp頁面(ui我選用的是layui,前面文章有說關(guān)于springboot引入layui等相關(guān)js)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: wyh Date: 2021/4/20 Time: 21:45 短信發(fā)送 --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>短信發(fā)送</title> <!--引入layui的css--> <link rel="stylesheet" href="../layui/css/layui.css" rel="external nofollow" > <!--引入layui的js--> <script type="text/javascript" src="../layui/layui.js"></script> <!--引入jquery的js --> <script type="text/javascript" src="../jquery/jquery.js"></script> </head> <body> <center> <div id="model2" > <div class="layui-form-item input-item"> <input type="text" placeholder="請(qǐng)輸入手機(jī)號(hào)" autocomplete="off" id="memPhone" name="memPhone" class="layui-input" style="width: 10%;"> </div> <div class="layui-form-item input-item"> <input type="text" placeholder="請(qǐng)輸入驗(yàn)證碼" autocomplete="off" id="code" name="code" maxlength="6" class="layui-input" style="width: 10%;"> <input type="button" class="layui-btn layui-btn-primary" value="獲取驗(yàn)證碼" id="sendBtn" style="width:10%;border-color:#1e9fff !important;" onclick="sendCode()" ></input> </div> </div> </center> </body> <script> function sendCode(){ var memPhone = $("#memPhone").val(); if(memPhone == '' || memPhone.length != 11){ layer.msg("請(qǐng)輸入正確的手機(jī)號(hào)!"); return; }else{ $.ajax({ type: 'get', url: '/sendCode', 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); } } </script> </html>
一個(gè)簡(jiǎn)單的頁面樣式如下
輸入正確的手機(jī)號(hào)進(jìn)行測(cè)試
收到短信如下
這樣一個(gè)簡(jiǎn)單的自己代碼實(shí)現(xiàn)短信發(fā)送就完成啦,以下為官方的一些參數(shù)類型以及說明
到此這篇關(guān)于SpringBoot使用榛子云實(shí)現(xiàn)手機(jī)短信發(fā)送驗(yàn)證碼的文章就介紹到這了,更多相關(guān)SpringBoot短信發(fā)送驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ssm框架controller層返回json格式數(shù)據(jù)到頁面的實(shí)現(xiàn)
這篇文章主要介紹了ssm框架controller層返回json格式數(shù)據(jù)到頁面的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09解決Nacos集群?jiǎn)?dòng)失敗:java版本問題
這篇文章主要介紹了解決Nacos集群?jiǎn)?dòng)失敗:java版本問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06springboot處理url中帶斜杠/\字符的參數(shù)報(bào)400問題
這篇文章主要介紹了springboot處理url中帶斜杠/\字符的參數(shù)報(bào)400問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Java實(shí)現(xiàn)Huffman編碼的示例代碼
Huffman編碼是一種編碼方式,本文主要介紹了Java實(shí)現(xiàn)Huffman編碼的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08