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>

        四步,搞定一個短信驗證碼登錄!

        共 3815字,需瀏覽 8分鐘

         ·

        2021-02-06 12:34

        Java技術(shù)棧

        www.javastack.cn

        關(guān)注閱讀更多優(yōu)質(zhì)文章



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

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

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

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

        5、接收用戶填寫的驗證碼及其他數(shù)據(jù);

        6、對比提交的驗證碼與Session中的驗證碼是否一致,同時判斷提交動作是否在有效期內(nèi);

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

        一、首先添加一個jar包,工具類會用到。


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

        我這里只是編寫一個簡單的短信驗證功能,要是用其他的語音驗證。。。。下面是編寫的一個config文檔,專門存放一些參數(shù)。

        二、編寫http請求工具類

        public?class?HttpUtil
        {
        ???/**
        ????*?構(gòu)造通用參數(shù)timestamp、sig和respDataType
        ????*?
        ????*?@return
        ????*/
        ???public?static?String?createCommonParam()
        ???{
        ??????//?時間戳
        ??????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請求
        ????*?
        ????*?@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();

        ?????????//?讀取返回數(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)測試工具方法
        ????*?
        ????*?@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();

        ?????????//?讀取返回數(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(),便會發(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?=?"【小陶科技】登錄驗證碼:{"+runNumber().toString()+"},如非本人操作,請忽略此短信。";

        ???/**
        ????*?驗證碼通知短信
        ????*/
        ???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();

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

        作者:classabcd
        本文鏈接:https://blog.csdn.net/classabcd/article/details/82464582
        版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接和本聲明。






        關(guān)注Java技術(shù)??锤喔韶?/strong>



        戳原文,獲取精選面試題!
        瀏覽 69
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

          <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            久久国内精品 | 免费无遮挡婬乱A片 | 寂寞少妇野外野战正片 | 欧美日韩18 | 午夜一区二区免费 | 一级坐爱视频 | 摸了女同学的下面毛** | 伊人78| 欧美日韩精品一区二区在线播放 | 欧美老女人性生活视频 |