SMS verification code login process ideas and detailed steps

Part1 business process
1. Construct the mobile phone verification code: use the random object to generate the required random number as the verification code, for example, a 4-digit verification code: a random number between 1000 and 9999;
2. Use the interface to send the mobile phone number and verification code data to the SMS platform, and then the SMS platform sends the verification code to the designated mobile phone number. The interface parameters generally include: target mobile phone number, random verification code (or including expiration time), platform interface address, platform password;
3. Save the information returned by the interface (usually json text data, and then need to be converted to json object format);
4. Save the mobile phone number - verification code and operation time into the session for later verification;
5. Receive the verification code and other data filled in by the user;
6. Compare whether the submitted verification code is consistent with the verification code in the session, and judge whether the submitted action is within the validity period;
7. If the verification code is correct and within the validity period, the request is passed and the corresponding business is processed.

Part2 first add a jar package

tools will be used

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

Part3 Write a simple SMS verification function

I am just writing a simple SMS verification function here, if other voice verification is used. . . . Wait, you need to go to Miaodi Cloud to download the official document. The following is a config document written to store some parameters.

Part4 Writing http request tool class

public class HttpUtil
{
   /**
    * 构造通用参数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 "&timestamp=" + timestamp + "&sig=" + sig + "&respDataType=" + Config.RESP_DATA_TYPE;
   }

   /**
    * post请求
    *
    * @param url
    * 功能和操作
    * @param body
    * 要post的数据
    * @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();

         // 设置连接参数
         conn.setDoOutput(true);
         conn.setDoInput(true);
         conn.setConnectTimeout(5000);
         conn.setReadTimeout(20000);
         conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
         // 提交数据
         out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
         out.write(body);
         out.flush();

         // 读取返回数据
         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;
   }

   /**
    * 回调测试工具方法
    *
    * @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();

         // 设置连接参数
         conn.setDoOutput(true);
         conn.setDoInput(true);
         conn.setConnectTimeout(5000);
         conn.setReadTimeout(20000);

         // 提交数据
         out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
         out.write(body);
         out.flush();

         // 读取返回数据
         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 method of generating four digits


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、执行方法execute(),便会发送成功

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);
   }

The above is the detailed steps of the SMS verification code login process

Author | classabcd

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

If you see this, it means you like this article, please forward and like it. Search "web_resource" on WeChat, follow and reply to "join the group" or scan the QR code below to enter the ad-free exchange group.


   
   
   
   
   
↓扫描二维码进群↓

This article is shared from WeChat public account - Java backend (web_resource).
If there is any infringement, please contact [email protected] to delete it.
This article participates in the " OSC Yuanchuang Project ", you are welcome to join and share with us.

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/3694624/blog/5254955