<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          搞一個(gè)短信驗(yàn)證碼登錄,難嗎?(附源碼)

          共 3859字,需瀏覽 8分鐘

           ·

          2020-09-30 01:07

          點(diǎn)擊上方[全棧開(kāi)發(fā)者社區(qū)]右上角[...][設(shè)為星標(biāo)?]

          作者 :classabcd

          來(lái)源 :blog.csdn.net/classabcd/article/details/82464582

          1、構(gòu)造手機(jī)驗(yàn)證碼:使用random對(duì)象生成要求的隨機(jī)數(shù)作為驗(yàn)證碼,例如4位驗(yàn)證碼:1000~9999之間隨機(jī)數(shù);

          2、使用接口向短信平臺(tái)發(fā)送手機(jī)號(hào)和驗(yàn)證碼數(shù)據(jù),然后短信平臺(tái)再把驗(yàn)證碼發(fā)送到制定手機(jī)號(hào)上,接口參數(shù)一般包括:目標(biāo)手機(jī)號(hào),隨機(jī)驗(yàn)證碼(或包含失效時(shí)間),平臺(tái)接口地址,平臺(tái)口令;

          3、保存接口返回的信息(一般為json文本數(shù)據(jù),然后需轉(zhuǎn)換為json對(duì)象格式);

          4、將手機(jī)號(hào)--驗(yàn)證碼、操作時(shí)間存入Session中,作為后面驗(yàn)證使用;

          5、接收用戶(hù)填寫(xiě)的驗(yàn)證碼及其他數(shù)據(jù);

          6、對(duì)比提交的驗(yàn)證碼與Session中的驗(yàn)證碼是否一致,同時(shí)判斷提交動(dòng)作是否在有效期內(nèi);

          7、驗(yàn)證碼正確且在有效期內(nèi),請(qǐng)求通過(guò),處理相應(yīng)的業(yè)務(wù)。

          一,首先添加一個(gè)jar包,工具類(lèi)會(huì)用到。




          ??commons-codec
          ??commons-codec
          ??1.11

          二、我這里只是編寫(xiě)一個(gè)簡(jiǎn)單的短信驗(yàn)證功能,要是用其他的語(yǔ)音驗(yàn)證。。等等需要去秒滴云官方下載文檔,下面是編寫(xiě)的一個(gè)config文檔,專(zhuān)門(mén)存放一些參數(shù)

          三、編寫(xiě)http請(qǐng)求工具類(lèi)

          public?class?HttpUtil
          {
          ???/**
          ????*?構(gòu)造通用參數(shù)timestamp、sig和respDataType
          ????*
          ????*?@return
          ????*/
          ???public?static?String?createCommonParam()
          ???{
          ??????//?時(shí)間戳
          ??????SimpleDateFormat?sdf?=?new?SimpleDateFormat("yyyyMMddHHmmss");
          ??????String?timestamp?=?sdf.format(new?Date());


          ??????//?簽名
          ??????String?sig?=?DigestUtils.md5Hex(Config.ACCOUNT_SID?+?Config.AUTH_TOKEN?+?timestamp);


          ??????return?"×tamp="?+?timestamp?+?"&sig="?+?sig?+?"&respDataType="?+?Config.RESP_DATA_TYPE;
          ???}


          ???/**
          ????*?post請(qǐng)求
          ????*
          ????*?@param?url
          ????*?功能和操作
          ????*?@param?body
          ????*?要post的數(shù)據(jù)
          ????*?@return
          ????*?@throws?IOException
          ????*/
          ???public?static?String?post(String?url,?String?body)
          ???{
          ??????System.out.println("url:"?+?System.lineSeparator()?+?url);
          ??????System.out.println("body:"?+?System.lineSeparator()?+?body);


          ??????String?result?=?"";
          ??????try
          ??????{
          ?????????OutputStreamWriter?out?=?null;
          ?????????BufferedReader?in?=?null;
          ?????????URL?realUrl?=?new?URL(url);
          ?????????URLConnection?conn?=?realUrl.openConnection();


          ?????????//?設(shè)置連接參數(shù)
          ?????????conn.setDoOutput(true);
          ?????????conn.setDoInput(true);
          ?????????conn.setConnectTimeout(5000);
          ?????????conn.setReadTimeout(20000);
          ?????????conn.setRequestProperty("Content-Type",?"application/x-www-form-urlencoded");
          ?????????//?提交數(shù)據(jù)
          ?????????out?=?new?OutputStreamWriter(conn.getOutputStream(),?"UTF-8");
          ?????????out.write(body);
          ?????????out.flush();


          ?????????//?讀取返回?cái)?shù)據(jù)
          ?????????in?=?new?BufferedReader(new?InputStreamReader(conn.getInputStream(),?"UTF-8"));
          ?????????String?line?=?"";
          ?????????boolean?firstLine?=?true;?//?讀第一行不加換行符
          ?????????while?((line?=?in.readLine())?!=?null)
          ?????????{
          ????????????if?(firstLine)
          ????????????{
          ???????????????firstLine?=?false;
          ????????????}?else
          ????????????{
          ???????????????result?+=?System.lineSeparator();
          ????????????}
          ????????????result?+=?line;
          ?????????}
          ??????}?catch?(Exception?e)
          ??????{
          ?????????e.printStackTrace();
          ??????}
          ??????return?result;
          ???}


          ???/**
          ????*?回調(diào)測(cè)試工具方法
          ????*
          ????*?@param?url
          ????*?@param?reqStr
          ????*?@return
          ????*/
          ???public?static?String?postHuiDiao(String?url,?String?body)
          ???{
          ??????String?result?=?"";
          ??????try
          ??????{
          ?????????OutputStreamWriter?out?=?null;
          ?????????BufferedReader?in?=?null;
          ?????????URL?realUrl?=?new?URL(url);
          ?????????URLConnection?conn?=?realUrl.openConnection();


          ?????????//?設(shè)置連接參數(shù)
          ?????????conn.setDoOutput(true);
          ?????????conn.setDoInput(true);
          ?????????conn.setConnectTimeout(5000);
          ?????????conn.setReadTimeout(20000);


          ?????????//?提交數(shù)據(jù)
          ?????????out?=?new?OutputStreamWriter(conn.getOutputStream(),?"UTF-8");
          ?????????out.write(body);
          ?????????out.flush();


          ?????????//?讀取返回?cái)?shù)據(jù)
          ?????????in?=?new?BufferedReader(new?InputStreamReader(conn.getInputStream(),?"UTF-8"));
          ?????????String?line?=?"";
          ?????????boolean?firstLine?=?true;?//?讀第一行不加換行符
          ?????????while?((line?=?in.readLine())?!=?null)
          ?????????{
          ????????????if?(firstLine)
          ????????????{
          ???????????????firstLine?=?false;
          ????????????}?else
          ????????????{
          ???????????????result?+=?System.lineSeparator();
          ????????????}
          ????????????result?+=?line;
          ?????????}
          ??????}?catch?(Exception?e)
          ??????{
          ?????????e.printStackTrace();
          ??????}
          ??????return?result;
          ???}
          }

          四、生成四位數(shù)的方法

          public?static?String?runNumber()?{
          ???String?str="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
          ???StringBuilder?sb=new?StringBuilder(4);
          ???for(int?i=0;i<4;i++)
          ???{
          ??????char?ch=str.charAt(new?Random().nextInt(str.length()));
          ??????sb.append(ch);
          ???}
          ???System.out.println(sb.toString());
          ???String?code?=?sb.toString();
          ???return?code;
          }

          五、執(zhí)行方法execute(),便會(huì)發(fā)送成功

          public?class?IndustrySMS
          {
          ???private?static?String?operation?=?"/industrySMS/sendSMS";


          ???private?static?String?accountSid?=?Config.ACCOUNT_SID;
          ???private?static?String?to?=?"15342349382";
          ??private?static?String?smsContent?=?"【小陶科技】登錄驗(yàn)證碼:{"+runNumber().toString()+"},如非本人操作,請(qǐng)忽略此短信。";


          ???/**
          ????*?驗(yàn)證碼通知短信
          ????*/
          ???public?static?void?execute()
          ???{
          ??????String?tmpSmsContent?=?null;
          ???????try{
          ?????????tmpSmsContent?=?URLEncoder.encode(smsContent,?"UTF-8");
          ???????}catch(Exception?e){
          ???????}
          ???????String?url?=?Config.BASE_URL?+?operation;
          ???????String?body?=?"accountSid="?+?accountSid?+?"&to="?+?to?+?"&smsContent="?+?tmpSmsContent
          ???????????+?HttpUtil.createCommonParam();


          ???????//?提交請(qǐng)求
          ???????String?result?=?HttpUtil.post(url,?body);
          ???????System.out.println("result:"?+?System.lineSeparator()?+?result);
          }


          薦書(shū):億級(jí)流量Java高并發(fā)與網(wǎng)絡(luò)編程實(shí)戰(zhàn)

          編輯推薦:

          (1)全面。本書(shū)從并發(fā)的底層核心技術(shù)、互聯(lián)網(wǎng)應(yīng)用框架、數(shù)據(jù)處理等三部分對(duì)高并發(fā)系列技術(shù)做了系統(tǒng)講解。?
          (2)實(shí)用。本書(shū)以實(shí)戰(zhàn)化訓(xùn)練為宗旨,用詳盡且經(jīng)典的案例闡述了 Java大數(shù)據(jù)及高級(jí)編程中的重點(diǎn)、難點(diǎn)。書(shū)中案例由真實(shí)項(xiàng)目演化而來(lái),既體現(xiàn)了所述知識(shí)點(diǎn)的精華,又屏蔽了無(wú)關(guān)技術(shù)的干擾。?
          (3)案例完整。案例都是以“理論講解 + 環(huán)境搭建 + 完整代碼及分析 + 運(yùn)行截圖”這種完善的結(jié)構(gòu)進(jìn)行講解,考慮到了讀者可能會(huì)遇到的各種問(wèn)題。


          如何購(gòu)買(mǎi):閱讀原文購(gòu)買(mǎi),也可堅(jiān)持留言打卡獲得!

          如何贈(zèng)送:留言集贊數(shù)大于30贊且排名前三的同學(xué)各贈(zèng)送一本,定價(jià)128RMB。


          覺(jué)得本文對(duì)你有幫助?請(qǐng)分享給更多人

          關(guān)注「全棧開(kāi)發(fā)者社區(qū)」加星標(biāo),提升全棧技能


          本公眾號(hào)會(huì)不定期給大家發(fā)福利,包括送書(shū)、學(xué)習(xí)資源等,敬請(qǐng)期待吧!

          如果感覺(jué)推送內(nèi)容不錯(cuò),不妨右下角點(diǎn)個(gè)在看轉(zhuǎn)發(fā)朋友圈或收藏,感謝支持。


          好文章,留言、點(diǎn)贊、在看和分享一條龍吧??

          瀏覽 31
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  色五月亚洲| 操逼做爱视| 亚洲无码性爱video | 中日韩无码视频 | 欧美性爱 在线 |