SpringBoot發(fā)送短信驗證碼的實例
更新時間:2022年02月21日 14:55:44 作者:芝蘭生于深谷
第三方短信發(fā)送平臺有很多種,各個平臺有各自的優(yōu)缺點,在選擇的時候可以根據(jù)自己的具體實際情況定奪,本文主要介紹了SpringBoot發(fā)送短信驗證碼的實例,感興趣的可以了解一下
1、注冊短信通賬號
網(wǎng)址:http://sms.webchinese.cn
2、導(dǎo)入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-java8time</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> <!--短信驗證依賴導(dǎo)入--> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.0.1</version> </dependency>
3、隨機驗證碼的工具類
public class CodeUtil { @Bean public PasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } /** * 隨機生成6位驗證碼 * */ public String getRandomCode(Integer code){ Random random = new Random(); StringBuffer result= new StringBuffer(); for (int i=0;i<code;i++){ result.append(random.nextInt(10)); } return result.toString(); } //保存驗證碼和時間 public Code saveCode(String code){ Code code1=new Code(); SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); Calendar c = Calendar.getInstance(); c.add(Calendar.MINUTE, 5); String currentTime = sf.format(c.getTime());// 生成5分鐘后時間,用戶校驗是否過期 //驗證碼加密 String encode=passwordEncoder().encode(code); code1.setCode(encode); code1.setCurrentTime(currentTime); return code1; } /** * 解密處理 * @param code 輸入 * @param code1 目標(biāo) */ public Boolean deciphering(String code,String code1){ boolean matches = passwordEncoder().matches(code,code1); return matches; } }
4、短信發(fā)送工具類
@Component public class TelMessageUtil { private static final String SMS_Url = "http://sms.webchinese.cn/web_api/"; /** * @param Uid SMS用戶id * @param Key 接口秘鑰:SMS登錄可查(非登錄密碼) * @param sendPhoneNum 短信發(fā)送目標(biāo)號碼 * @param desc 短信內(nèi)容 * @return Integer(1:成功碼,其他失敗,具體參見注釋) */ public static Integer send(String Uid,String Key,String sendPhoneNum,String desc){ HttpClient client = new HttpClient(); PostMethod post = new PostMethod(SMS_Url); post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");// 在頭文件中設(shè)置轉(zhuǎn)碼 //設(shè)置參數(shù) NameValuePair[] data = { new NameValuePair("Uid", Uid), new NameValuePair("Key", Key),//秘鑰 new NameValuePair("smsMob", sendPhoneNum), new NameValuePair("smsText", desc) }; post.setRequestBody(data); try { client.executeMethod(post); } catch (Exception e) { e.printStackTrace(); } Header[] headers = post.getResponseHeaders(); int statusCode = post.getStatusCode(); System.out.println("statusCode:" + statusCode); for (Header h : headers) { System.out.println(h.toString()); } String result =""; try { result = new String(post.getResponseBodyAsString().getBytes("gbk")); } catch (Exception e) { e.printStackTrace(); } post.releaseConnection(); return Integer.parseInt(result); } /** * -1 沒有該用戶賬戶 -2 接口密鑰不正確 [查看密鑰]不是賬戶登陸密碼 -21 MD5接口密鑰加密不正確 -3 短信數(shù)量不足 -11 該用戶被禁用 -14 短信內(nèi)容出現(xiàn)非法字符 -4 手機號格式不正確 -41 手機號碼為空 -42 短信內(nèi)容為空 -51 短信簽名格式不正確接口簽名格式為:【簽名內(nèi)容】 -6 IP限制 大于0 短信發(fā)送數(shù)量 以上作為補充 */ public static String getMessage(Integer code){ String message; if(code > 0 ) { message = "SMS-f發(fā)送成功!短信量還有" + code + "條"; }else if(code == -1){ message = "SMS-沒有該用戶賬戶"; }else if(code == -2){ message = "SMS-接口密鑰不正確"; }else if(code == -21){ message = "SMS-MD5接口密鑰加密不正確"; }else if(code == -3){ message = "SMS-短信數(shù)量不足"; }else if(code == -11){ message = "SMS-該用戶被禁用"; }else if(code == -14){ message = "SMS-短信內(nèi)容出現(xiàn)非法字符"; }else if(code == -4){ message = "SMS-手機號格式不正確"; }else if(code == -41){ message = "SMS-手機號碼為空"; }else if(code == -42){ message = "SMS-短信內(nèi)容為空"; }else if(code == -51){ message = "SMS-短信簽名格式不正確接口簽名格式為:【簽名內(nèi)容】"; }else if(code == -6){ message = "SMS-IP限制"; }else{ message = "其他錯誤"; } return message; } }
5、測試
//前臺驗證手機號 @RequestMapping("/checkTel") public String checkTel(@RequestParam("tel") String tel, HttpSession session, Model model){ Users users=new Users(); users.setTel(tel); if (loginService.CheckOnly(users)) { model.addAttribute("msg","沒有此用戶,請注冊!"); return "register"; }else { model.addAttribute("info","已向你的手機號為"+tel+"發(fā)送了驗證碼"); Long id = loginService.findIdByTel(tel); session.setAttribute("user_id",id); //發(fā)送驗證碼 CodeUtil codeUtil=new CodeUtil(); //獲取六位驗證碼 String randomCode = codeUtil.getRandomCode(6); //先清除session域 session.removeAttribute("checkCode"); //加密驗證碼存放session域中 session.setAttribute("checkCode",codeUtil.passwordEncoder().encode(randomCode)); Integer resultCode = TelMessageUtil.send("****","************",tel, "【高校志愿者平臺】你的驗證碼為【"+randomCode+"】(若不是本人操作,可忽略該條郵件)" ); System.out.println("日志信息"+resultCode); return "updatePwd"; } }
到此這篇關(guān)于SpringBoot發(fā)送短信驗證碼的實例的文章就介紹到這了,更多相關(guān)SpringBoot 短信驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Cloud?Gateway?整合?knife4j?聚合接口文檔功能
這篇文章主要介紹了Spring?Cloud?Gateway?整合?knife4j?聚合接口文檔的相關(guān)知識,我們可以基于?Spring?Cloud?Gateway?網(wǎng)關(guān)?+?nacos?+?knife4j?對所有微服務(wù)項目的接口文檔進行聚合,從而實現(xiàn)我們想要的文檔管理功能,需要的朋友可以參考下2022-02-02Java實現(xiàn)添加、驗證PDF數(shù)字簽名的方法示例
在設(shè)置文檔內(nèi)容保護的方法中,除了對文檔加密、添加水印外,應(yīng)用數(shù)字簽名也是一種有效防偽手段。本文就使用Java實現(xiàn)添加、驗證PDF數(shù)字簽名,感興趣的可以了解一下2021-07-07IntelliJ IDEA使用教程從入門到上癮(2019圖文版)
這篇文章主要介紹了IntelliJ IDEA使用教程從入門到上癮(2019圖文版),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12java實現(xiàn)系統(tǒng)多級文件夾復(fù)制
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)系統(tǒng)多級文件夾復(fù)制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08MybatisPlus EntityWrapper如何自定義SQL
這篇文章主要介紹了MybatisPlus EntityWrapper如何自定義SQL,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03