農林漁牧網

您現在的位置是:首頁 > 畜牧業

搞一個簡訊驗證碼登入,難嗎?

2021-12-18由 碼農突圍666 發表于 畜牧業

簡訊平臺傳送驗證碼怎麼操作

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

2、使用介面向簡訊平臺傳送手機號和驗證碼資料,然後簡訊平臺再把驗證碼傳送到制定手機號上,介面引數一般包括:目標手機號,隨機驗證碼(或包含失效時間),平臺介面地址,平臺口令;

3、儲存介面返回的資訊(一般為json文字資料,然後需轉換為json物件格式);

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

5、接收使用者填寫的驗證碼及其他資料;

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

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

一,首先新增一個jar包,工具類會用到。

<!——秒滴雲的jar包——> commons-codec commons-codec 1。11

二、我這裡只是編寫一個簡單的簡訊驗證功能,要是用其他的語音驗證。。。。等等需要去秒滴雲官方下載文件,下面是編寫的一個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);}