1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        短信驗(yàn)證碼登錄流程思路及詳細(xì)步驟

        共 11783字,需瀏覽 24分鐘

         ·

        2021-09-07 10:53

        作者 | classabcd

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

        Part1業(yè)務(wù)流程

        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ù)。

        Part2首先添加一個(gè)jar包

        工具類(lèi)會(huì)用到

        <!--秒滴云的jar包-->
        <dependency>
          <groupId>commons-codec</groupId>
          <artifactId>commons-codec</artifactId>
          <version>1.11</version>
        </dependency>

        Part3編寫(xiě)簡(jiǎn)單的短信驗(yàn)證功能

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

        如果您正在學(xué)習(xí)Spring Boot,推薦一個(gè)連載多年還在繼續(xù)更新的免費(fèi)教程:http://blog.didispace.com/spring-boot-learning-2x/

        Part4編寫(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 "&timestamp=" + 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;
           }
        }

        Part5生成四位數(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;
        }
        4、執(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);
           }

        以上就是短信驗(yàn)證碼登錄流程詳細(xì)步驟

        —————END—————

        往期推薦

        反996,竟反掉了17%的薪水?!而隔壁的房貸還在漲,怎么就越反越糟糕了呢?

        假如 IDEA 也加入防沉迷功能...

        說(shuō)說(shuō)延時(shí)隊(duì)列實(shí)現(xiàn)的幾種姿勢(shì)

        有個(gè)程序員老公有多爽???

        飽受爭(zhēng)議的GitHub Copilot 神操作:自動(dòng)補(bǔ)全補(bǔ)出了B站CEO的身份證!



        喜歡本文歡迎轉(zhuǎn)發(fā),關(guān)注我訂閱更多精彩

        關(guān)注我回復(fù)「加群」,加入Spring技術(shù)交流群

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

        手機(jī)掃一掃分享

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

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            国内自拍偷拍视频 | 精品无码一级毛片免费 | 日逼激情网 | 护士被老头添一夜视频播放 | 欧美做爰BBB性BBBBB丨D | 逼中极品一线天 | 看一级黄色片子 | 搜索黄色小说 | 粉嫩欧美一区二区三区高清影视 | 伦理视频在线观看夫妻板国产 |