短信驗證碼登錄流程詳細步驟


1、構造手機驗證碼:使用random對象生成要求的隨機數作為驗證碼,例如4位驗證碼:1000~9999之間隨機數;

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

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

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

5、接收用戶填寫的驗證碼及其他數據;

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

7、驗證碼正確且在有效期內,請求通過,處理相應的業務。

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

1 <!--秒滴雲的jar包-->
2 <dependency>
3   <groupId>commons-codec</groupId>
4   <artifactId>commons-codec</artifactId>
5   <version>1.11</version>
6 </dependency>

二、我這里只是編寫一個簡單的短信驗證功能,要是用其他的語音驗證。。。。等等需要去秒滴雲官方下載文檔,下面是編寫的一個config文檔,專門存放一些參數

 

 二、編寫http請求工具類

  1 public class HttpUtil
  2 {
  3    /**
  4     * 構造通用參數timestamp、sig和respDataType
  5     *
  6     * @return
  7     */
  8    public static String createCommonParam()
  9    {
 10       // 時間戳
 11       SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
 12       String timestamp = sdf.format(new Date());
 13 
 14       // 簽名
 15       String sig = DigestUtils.md5Hex(Config.ACCOUNT_SID + Config.AUTH_TOKEN + timestamp);
 16 
 17       return "&timestamp=" + timestamp + "&sig=" + sig + "&respDataType=" + Config.RESP_DATA_TYPE;
 18    }
 19 
 20    /**
 21     * post請求
 22     *
 23     * @param url
 24     * 功能和操作
 25     * @param body
 26     * 要post的數據
 27     * @return
 28     * @throws IOException
 29     */
 30    public static String post(String url, String body)
 31    {
 32       System.out.println("url:" + System.lineSeparator() + url);
 33       System.out.println("body:" + System.lineSeparator() + body);
 34 
 35       String result = "";
 36       try
 37       {
 38          OutputStreamWriter out = null;
 39          BufferedReader in = null;
 40          URL realUrl = new URL(url);
 41          URLConnection conn = realUrl.openConnection();
 42 
 43          // 設置連接參數
 44          conn.setDoOutput(true);
 45          conn.setDoInput(true);
 46          conn.setConnectTimeout(5000);
 47          conn.setReadTimeout(20000);
 48          conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 49          // 提交數據
 50          out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
 51          out.write(body);
 52          out.flush();
 53 
 54          // 讀取返回數據
 55          in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
 56          String line = "";
 57          boolean firstLine = true; // 讀第一行不加換行符
 58          while ((line = in.readLine()) != null)
 59          {
 60             if (firstLine)
 61             {
 62                firstLine = false;
 63             } else
 64             {
 65                result += System.lineSeparator();
 66             }
 67             result += line;
 68          }
 69       } catch (Exception e)
 70       {
 71          e.printStackTrace();
 72       }
 73       return result;
 74    }
 75 
 76    /**
 77     * 回調測試工具方法
 78     *
 79     * @param url
 80     * @param reqStr
 81     * @return
 82     */
 83    public static String postHuiDiao(String url, String body)
 84    {
 85       String result = "";
 86       try
 87       {
 88          OutputStreamWriter out = null;
 89          BufferedReader in = null;
 90          URL realUrl = new URL(url);
 91          URLConnection conn = realUrl.openConnection();
 92 
 93          // 設置連接參數
 94          conn.setDoOutput(true);
 95          conn.setDoInput(true);
 96          conn.setConnectTimeout(5000);
 97          conn.setReadTimeout(20000);
 98 
 99          // 提交數據
100          out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
101          out.write(body);
102          out.flush();
103 
104          // 讀取返回數據
105          in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
106          String line = "";
107          boolean firstLine = true; // 讀第一行不加換行符
108          while ((line = in.readLine()) != null)
109          {
110             if (firstLine)
111             {
112                firstLine = false;
113             } else
114             {
115                result += System.lineSeparator();
116             }
117             result += line;
118          }
119       } catch (Exception e)
120       {
121          e.printStackTrace();
122       }
123       return result;
124    }
125 }

三、生成四位數的方法

 1 public static String runNumber() {
 2    String str="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 3    StringBuilder sb=new StringBuilder(4);
 4    for(int i=0;i<4;i++)
 5    {
 6       char ch=str.charAt(new Random().nextInt(str.length()));
 7       sb.append(ch);
 8    }
 9    System.out.println(sb.toString());
10    String code = sb.toString();
11    return code;
12 }

4、執行方法execute(),便會發送成功

 1 public class IndustrySMS
 2 {
 3    private static String operation = "/industrySMS/sendSMS";
 4 
 5    private static String accountSid = Config.ACCOUNT_SID;
 6    private static String to = "15342349382";
 7    private static String smsContent = "【小陶科技】登錄驗證碼:{"+runNumber().toString()+"},如非本人操作,請忽略此短信。";
 8 
 9    /**
10     * 驗證碼通知短信
11     */
12    public static void execute()
13    {
14       String tmpSmsContent = null;
15        try{
16          tmpSmsContent = URLEncoder.encode(smsContent, "UTF-8");
17        }catch(Exception e){
18          
19        }
20        String url = Config.BASE_URL + operation;
21        String body = "accountSid=" + accountSid + "&to=" + to + "&smsContent=" + tmpSmsContent
22            + HttpUtil.createCommonParam();
23 
24        // 提交請求
25        String result = HttpUtil.post(url, body);
26        System.out.println("result:" + System.lineSeparator() + result);
27    }

參考:

blog.csdn.net/classabcd/article/details/82464582


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM