1
、構造手機驗證碼:使用 random 對象生成要求的隨機數作為驗證碼,例如 4 位驗證碼: 1000~9999 之間隨機數;
2
、使用接口向短信平臺發送手機號和驗證碼數據,然后短信平臺再把驗證碼發送到制定手機號上,接口參數一般包括:目標手機號,隨機驗證碼 (或包含失效時間),平臺接口地址,平臺口令;
3
、保存接口返回的信息(一般為 json 文本數據,然后需轉換為 json 對象格式);
4
、將手機號 — 驗證碼、操作時間存入 Session 中,作為后面驗證使用;
5
、接收用戶填寫的驗證碼及其他數據;
6
、對比提交的驗證碼與 Session 中的驗證碼是否一致,同時判斷提交動作是否在有效期內;
7
、驗證碼正確且在有效期內,請求通過,處理相應的業務。
一,首先添加一個 jar 包,工具類會用到。
- <!--秒滴云的jar包-->
- <dependency>
- <groupId>commons-codec</groupId>
- <artifactId>commons-codec</artifactId>
- <version>1.11</version>
- </dependency>
二、我這里只是編寫一個簡單的短信驗證功能,要是用其他的語音驗證。
等等需要去秒滴云官方下載文檔,下面是編寫的一個 config 文檔,專門存放一些參數
三、編寫 http 請求工具類
- 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 "×tamp=" + 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;
- }
- }
四、生成四位數的方法
- 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;
- }
執行方法 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);
- }
到此這篇關于springboot短信驗證碼登錄功能的實現的文章就介紹到這了,更多相關springboot短信驗證碼登錄內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!