SpringBoot發(fā)送短信驗(yàn)證碼的實(shí)例
更新時(shí)間:2022年02月21日 14:55:44 作者:芝蘭生于深谷
第三方短信發(fā)送平臺(tái)有很多種,各個(gè)平臺(tái)有各自的優(yōu)缺點(diǎn),在選擇的時(shí)候可以根據(jù)自己的具體實(shí)際情況定奪,本文主要介紹了SpringBoot發(fā)送短信驗(yàn)證碼的實(shí)例,感興趣的可以了解一下
1、注冊(cè)短信通賬號(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","沒有此用戶,請(qǐng)注冊(cè)!");
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)容請(qǐng)搜索腳本之家以前的文章或繼續(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)注冊(cè)功能
- 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創(chuàng)建項(xiàng)目的圖文教程(idea版本)
這篇文章主要介紹了Springboot創(chuàng)建項(xiàng)目的圖文教程(idea版本),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Java的Jackson庫中復(fù)雜對(duì)象集合的幾種簡單轉(zhuǎn)換
本文主要介紹了Java的Jackson庫中復(fù)雜對(duì)象集合的幾種簡單轉(zhuǎn)換。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02
IDEA 2021配置JavaWeb項(xiàng)目超詳細(xì)教程
本文通過圖文并茂的形式給大家介紹IDEA 2021配置JavaWeb項(xiàng)目的過程,內(nèi)容簡單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-08-08
Springboot如何根據(jù)實(shí)體類生成數(shù)據(jù)庫表
這篇文章主要介紹了Springboot如何根據(jù)實(shí)體類生成數(shù)據(jù)庫表的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09

