SpringBoot發(fā)送短信驗(yàn)證碼的實(shí)例
1、注冊短信通賬號(hào)
網(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>
<!--短信驗(yàn)證依賴導(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、隨機(jī)驗(yàn)證碼的工具類
public class CodeUtil {
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
/**
* 隨機(jī)生成6位驗(yàn)證碼
*
*/
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();
}
//保存驗(yàn)證碼和時(shí)間
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分鐘后時(shí)間,用戶校驗(yàn)是否過期
//驗(yàn)證碼加密
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)號(hà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 手機(jī)號(hào)格式不正確
-41 手機(jī)號(hào)碼為空
-42 短信內(nèi)容為空
-51 短信簽名格式不正確接口簽名格式為:【簽名內(nèi)容】
-6 IP限制
大于0 短信發(fā)送數(shù)量
以上作為補(bǔ)充
*/
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-手機(jī)號(hào)格式不正確";
}else if(code == -41){
message = "SMS-手機(jī)號(hào)碼為空";
}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 = "其他錯(cuò)誤";
}
return message;
}
}
5、測試
//前臺(tái)驗(yàn)證手機(jī)號(hào)
@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","已向你的手機(jī)號(hào)為"+tel+"發(fā)送了驗(yàn)證碼");
Long id = loginService.findIdByTel(tel);
session.setAttribute("user_id",id);
//發(fā)送驗(yàn)證碼
CodeUtil codeUtil=new CodeUtil();
//獲取六位驗(yàn)證碼
String randomCode = codeUtil.getRandomCode(6);
//先清除session域
session.removeAttribute("checkCode");
//加密驗(yàn)證碼存放session域中
session.setAttribute("checkCode",codeUtil.passwordEncoder().encode(randomCode));
Integer resultCode = TelMessageUtil.send("****","************",tel,
"【高校志愿者平臺(tái)】你的驗(yàn)證碼為【"+randomCode+"】(若不是本人操作,可忽略該條郵件)"
);
System.out.println("日志信息"+resultCode);
return "updatePwd";
}
}
到此這篇關(guān)于SpringBoot發(fā)送短信驗(yàn)證碼的實(shí)例的文章就介紹到這了,更多相關(guān)SpringBoot 短信驗(yàn)證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot發(fā)送html郵箱驗(yàn)證碼功能
- SpringBoot使用榛子云實(shí)現(xiàn)手機(jī)短信發(fā)送驗(yàn)證碼
- SpringBoot發(fā)送郵件功能 驗(yàn)證碼5分鐘過期
- SpringBoot使用郵箱發(fā)送驗(yàn)證碼實(shí)現(xiàn)注冊功能
- SpringBoot發(fā)送郵箱驗(yàn)證碼功能
- SpringBoot+Security 發(fā)送短信驗(yàn)證碼的實(shí)現(xiàn)
- Springboot實(shí)現(xiàn)阿里云通信短信服務(wù)有關(guān)短信驗(yàn)證碼的發(fā)送功能
- SpringBoot實(shí)現(xiàn)發(fā)送驗(yàn)證碼功能(圖片驗(yàn)證碼)
相關(guān)文章
SpringBoot超詳細(xì)講解自動(dòng)配置原理
在進(jìn)行項(xiàng)目編寫前,我們還需要知道一個(gè)東西,就是SpringBoot對我們的SpringMVC還做了哪些配置,包括如何擴(kuò)展,如何定制,只有把這些都搞清楚了,我們在之后使用才會(huì)更加得心應(yīng)手2022-06-06
Java實(shí)現(xiàn)文件檢索系統(tǒng)的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何劉Java語言實(shí)現(xiàn)簡易的文件檢索系統(tǒng),文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java開發(fā)有一定的幫助,需要的可以參考一下2022-07-07
Spring?Cloud?Gateway?整合?knife4j?聚合接口文檔功能
這篇文章主要介紹了Spring?Cloud?Gateway?整合?knife4j?聚合接口文檔的相關(guān)知識(shí),我們可以基于?Spring?Cloud?Gateway?網(wǎng)關(guān)?+?nacos?+?knife4j?對所有微服務(wù)項(xiàng)目的接口文檔進(jìn)行聚合,從而實(shí)現(xiàn)我們想要的文檔管理功能,需要的朋友可以參考下2022-02-02
Java實(shí)現(xiàn)添加、驗(yàn)證PDF數(shù)字簽名的方法示例
在設(shè)置文檔內(nèi)容保護(hù)的方法中,除了對文檔加密、添加水印外,應(yīng)用數(shù)字簽名也是一種有效防偽手段。本文就使用Java實(shí)現(xiàn)添加、驗(yàn)證PDF數(shù)字簽名,感興趣的可以了解一下2021-07-07
IntelliJ IDEA使用教程從入門到上癮(2019圖文版)
這篇文章主要介紹了IntelliJ IDEA使用教程從入門到上癮(2019圖文版),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
java實(shí)現(xiàn)系統(tǒng)多級(jí)文件夾復(fù)制
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)系統(tǒng)多級(jí)文件夾復(fù)制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
MybatisPlus EntityWrapper如何自定義SQL
這篇文章主要介紹了MybatisPlus EntityWrapper如何自定義SQL,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
如何將IDEA打成jar包并在windows后臺(tái)運(yùn)行
在本篇文章里小編給大家分享的是關(guān)于如何將IDEA打成jar包并在windows后臺(tái)運(yùn)行知識(shí)點(diǎn),需要的朋友們可以學(xué)習(xí)參考下。2019-08-08

