What is the login process of SMS verification code during java development?

  1. Construct a 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 formulated mobile phone number. The interface parameters generally include: target mobile phone number, random verification code (or include expiration time), platform interface Address, platform password;
  3. Save the information returned by the interface (generally json text data, which needs to be converted to json object format);
  4. Store the mobile phone number-verification code and operation time in the Session for later verification;
  5. Receive verification codes and other data filled in by users;
  6. Compare whether the verification code submitted is consistent with the verification code in the Session, and determine whether the submission action is within the validity period;
  7. The verification code is correct and within the validity period, the request is passed and the corresponding business is processed.
1, first add a jar package, the tool class will be used.
<!--Secondary cloud jar package-->
<dependency>
  <groupId>commons-codec</groupId>
  <artifactId>commons-codec</artifactId>
  <version>1.11</version>
</dependency>

2, I just write a simple SMS verification function, if you use other voice verification. . . . Wait for the official download document of the second drop cloud, the following is a config file written specifically to store some parameters



3, write http request tool class

public class HttpUtil
{
   /**
    * Construct general parameters timestamp, sig and respDataType
    *
    * @return
    */
   public static String createCommonParam()
   {
      // timestamp
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
      String timestamp = sdf.format(new Date());


      // signature
      String sig = DigestUtils.md5Hex(Config.ACCOUNT_SID + Config.AUTH_TOKEN + timestamp);


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


   /**
    * post request
    *
    * @param url
    * Function and operation
    * @param body
    * Data to 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();


         // Set connection parameters
         conn.setDoOutput(true);
         conn.setDoInput(true);
         conn.setConnectTimeout(5000);
         conn.setReadTimeout(20000);
         conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
         // submit data
         out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
         out.write(body);
         out.flush();


         // read return data
         in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
         String line = "";
         boolean firstLine = true; // read the first line without newline
         while ((line = in.readLine()) != null)
         {
            if (firstLine)
            {
               firstLine = false;
            } else
            {
               result += System.lineSeparator();
            }
            result += line;
         }
      } catch (Exception e)
      {
         e.printStackTrace();
      }
      return result;
   }


   /**
    * Callback test tool method
    *
    * @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();


         // Set connection parameters
         conn.setDoOutput(true);
         conn.setDoInput(true);
         conn.setConnectTimeout(5000);
         conn.setReadTimeout(20000);


         // submit data
         out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
         out.write(body);
         out.flush();


         // read return data
         in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
         String line = "";
         boolean firstLine = true; // read the first line without newline
         while ((line = in.readLine()) != null)
         {
            if (firstLine)
            {
               firstLine = false;
            } else
            {
               result += System.lineSeparator();
            }
            result += line;
         }
      } catch (Exception e)
      {
         e.printStackTrace();
      }
      return result;
   }
}
4, the 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;
}
5. Execute method execute(), it will be sent successfully

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 = "[小陶科技] Login verification code: {"+runNumber().toString()+"}, if not operated by yourself, please ignore this message.";


   /**
    * Verification code notification SMS
    */
   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();


       // Submit a request
       String result = HttpUtil.post(url, body);
       System.out.println("result:" + System.lineSeparator() + result);
  }

By Receive sms online.

If you need to receive sms free online, please visit https://receive-sms-free.net/