springboot短信驗(yàn)證碼登錄功能的實(shí)現(xiàn)
1
、構(gòu)造手機(jī)驗(yàn)證碼:使用 random 對象生成要求的隨機(jī)數(shù)作為驗(yàn)證碼,例如 4 位驗(yàn)證碼: 1000~9999 之間隨機(jī)數(shù);
2
、使用接口向短信平臺發(fā)送手機(jī)號和驗(yàn)證碼數(shù)據(jù),然后短信平臺再把驗(yàn)證碼發(fā)送到制定手機(jī)號上,接口參數(shù)一般包括:目標(biāo)手機(jī)號,隨機(jī)驗(yàn)證碼 (或包含失效時間),平臺接口地址,平臺口令;
3
、保存接口返回的信息(一般為 json 文本數(shù)據(jù),然后需轉(zhuǎn)換為 json 對象格式);
4
、將手機(jī)號 — 驗(yàn)證碼、操作時間存入 Session 中,作為后面驗(yàn)證使用;
5
、接收用戶填寫的驗(yàn)證碼及其他數(shù)據(jù);
6
、對比提交的驗(yàn)證碼與 Session 中的驗(yàn)證碼是否一致,同時判斷提交動作是否在有效期內(nèi);
7
、驗(yàn)證碼正確且在有效期內(nèi),請求通過,處理相應(yīng)的業(yè)務(wù)。
一,首先添加一個 jar 包,工具類會用到。
<!--秒滴云的jar包--> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.11</version> </dependency>
二、我這里只是編寫一個簡單的短信驗(yàn)證功能,要是用其他的語音驗(yàn)證。
等等需要去秒滴云官方下載文檔,下面是編寫的一個 config 文檔,專門存放一些參數(shù)
三、編寫 http 請求工具類
public class HttpUtil { /** * 構(gòu)造通用參數(shù)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的數(shù)據(jù) * @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(); // 設(shè)置連接參數(shù) conn.setDoOutput(true); conn.setDoInput(true); conn.setConnectTimeout(5000); conn.setReadTimeout(20000); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 提交數(shù)據(jù) out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); out.write(body); out.flush(); // 讀取返回數(shù)據(jù) 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; } /** * 回調(diào)測試工具方法 * * @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(); // 設(shè)置連接參數(shù) conn.setDoOutput(true); conn.setDoInput(true); conn.setConnectTimeout(5000); conn.setReadTimeout(20000); // 提交數(shù)據(jù) out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); out.write(body); out.flush(); // 讀取返回數(shù)據(jù) 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; } }
四、生成四位數(shù)的方法
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; }
執(zhí)行方法 execute(),便會發(fā)送成功
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 = "【小陶科技】登錄驗(yàn)證碼:{"+runNumber().toString()+"},如非本人操作,請忽略此短信。"; /** * 驗(yàn)證碼通知短信 */ 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); }
到此這篇關(guān)于springboot短信驗(yàn)證碼登錄功能的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot短信驗(yàn)證碼登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot實(shí)現(xiàn)攔截器之驗(yàn)證登錄示例
- SpringBoot + SpringSecurity 短信驗(yàn)證碼登錄功能實(shí)現(xiàn)
- Springboot實(shí)現(xiàn)驗(yàn)證碼登錄
- SpringBoot登錄驗(yàn)證碼實(shí)現(xiàn)過程詳解
- SpringBoot Security前后端分離登錄驗(yàn)證的實(shí)現(xiàn)
- vue+springboot實(shí)現(xiàn)登錄驗(yàn)證碼
- SpringBoot連接Microsoft SQL Server實(shí)現(xiàn)登錄驗(yàn)證
相關(guān)文章
java讀取文件顯示進(jìn)度條的實(shí)現(xiàn)方法
當(dāng)讀取一個大文件時,一時半會兒無法看到讀取結(jié)果,就需要顯示一個進(jìn)度條,是程序員明白已經(jīng)讀了多少文件,可以估算讀取還需要多少時間,下面的代碼可以實(shí)現(xiàn)這個功能2014-01-01Java實(shí)現(xiàn)自定義自旋鎖代碼實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)自定義自旋鎖代碼實(shí)例,Java自旋鎖是一種線程同步機(jī)制,它允許線程在獲取鎖時不立即阻塞,而是通過循環(huán)不斷嘗試獲取鎖,直到成功獲取為止,自旋鎖適用于鎖競爭激烈但持有鎖的時間很短的情況,需要的朋友可以參考下2023-10-10詳解Spring FactoryBean靈活創(chuàng)建復(fù)雜對象的秘密武器
FactoryBean是Spring框架中用于創(chuàng)建復(fù)雜Bean的接口,通過編程方式控制Bean的創(chuàng)建過程,它允許開發(fā)者自定義Bean的創(chuàng)建邏輯,適用于集成第三方庫、延遲初始化、動態(tài)代理和統(tǒng)一管理資源等場景,本文介紹Spring FactoryBean創(chuàng)建復(fù)雜對象的相關(guān)操作,感興趣的朋友一起看看吧2025-02-02Mybatis-plus多條件篩選分頁的實(shí)現(xiàn)
本文主要介紹了Mybatis-plus多條件篩選分頁,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09java實(shí)現(xiàn)幸運(yùn)抽獎系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)幸運(yùn)抽獎系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-07-07詳解Java弱引用(WeakReference)的理解與使用
這篇文章主要介紹了Java弱引用(WeakReference)的理解與使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04