詳細介紹Java阿里云的短信驗證碼實現(xiàn)
阿里云手機短信驗證碼
第一步 登錄阿里云開放平臺
1、進入阿里云開放平臺---->點擊控制臺

2、點擊AccessKey管理

3、點擊之后會彈出提示,選擇開始使用子用戶

4、新建一個用戶組,然后按要求填寫即可

5、創(chuàng)建一個用戶,按要求填寫內(nèi)容,勾選編程訪問


6、會得到AccessKey(id,密碼)
要將這個賬號保存下來!
//用戶登錄名稱 ==================== //AccessKey ID ==================== //AccessKey Secret ============================
第二步,開通阿里云短信服務
1、找到短信控制臺面板,點擊國內(nèi)消息

2、選擇模板管理,點擊添加模板

以下內(nèi)容按要求填寫即可,申請說明需要有正當理由不然審核可能通不過

點擊提交,等待審核
3、步驟同上,點擊簽名管理,添加簽名
注:簽名需要填寫應用名稱、網(wǎng)站名稱 例如:(dy學習網(wǎng)站) 個人用戶選擇適用場景的時候選擇驗證碼,申請說明需要填寫正當理由。不然可能審核不通過。

提交后等待審核通過即可。
第三步,編寫代碼測試
1、新建項目
創(chuàng)建一個Java項目,筆者創(chuàng)建的是SpringBoot項目
2、添加依賴
點擊快速學習——> 查看APIDemo,里面會有maven的依賴和官方的Demo

maven依賴:
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.0</version> </dependency>
3、編寫代碼
簡單測試,結(jié)合了redis的使用
controller:
package com.qxx.sendmessage.controller;
import com.qxx.sendmessage.service.SendMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* @author 東亞猛男Qxx
*/
@RestController
@CrossOrigin //跨域的支持
public class Controller {
@Autowired
private SendMessage sendMessage;
@Autowired
private RedisTemplate<String,String> template;
//RestFul風格請求
@GetMapping("/send/{phone}")
public String send(@PathVariable("phone") String phone){
//先看redis里面該手機號儲存的驗證碼是否失效
String code = template.opsForValue().get(phone);
if (!StringUtils.isEmpty(code)){
return phone+":"+"驗證碼尚未過期!";
}
//截取6為字符當作驗證碼(UUID)
code = UUID.randomUUID().toString().substring(0, 5);
HashMap<String,Object> map = new HashMap<>();
//key必須為code
map.put("code",code);
//調(diào)用service層的方法 傳兩個參數(shù):phone,map
Boolean flag = sendMessage.sendMessage(phone,map);
if (flag){
//存入redis,設置失效時間
template.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
return phone+":"+"驗證碼"+code+"發(fā)送成功!";
}
return "發(fā)送失敗";
}
}
service:
package com.qxx.sendmessage.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.qxx.sendmessage.service.SendMessage;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* @author 東亞猛男Qxx
*/
@Service
public class SendMessageImpl implements SendMessage {
@Override
public Boolean sendMessage(String phoneNum,Map<String, Object> map) {
System.out.println(JSONObject.toJSONString(map));
//連接阿里云,第一個參數(shù)不用改變,后兩個填寫自己的accessKeyId和accessSecret
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
IAcsClient client = new DefaultAcsClient(profile);
//構(gòu)建請求!
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain("dysmsapi.aliyuncs.com"); //不要動
request.setSysVersion("2017-05-25"); //不要動
request.setSysAction("SendSms");
//自定義參數(shù)(手機號,驗證碼,簽名,模板)
request.putQueryParameter("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumbers", phoneNum);
request.putQueryParameter("SignName", "簽名");
request.putQueryParameter("TemplateCode", "模板(SMS-*****)");
//構(gòu)建一個短信的驗證碼
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
return response.getHttpResponse().isSuccess();
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return false;
}
}
4、測試
此處用的是postman工具

redis:

短信:

結(jié)語:
可以編寫可復用的微服務接口,來實現(xiàn)驗證碼的發(fā)送,根據(jù)實際情況進行封裝
到此這篇關(guān)于詳細介紹Java阿里云的短信驗證碼實現(xiàn)的文章就介紹到這了,更多相關(guān)Java阿里云短信驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatisPlus?TypeHandler自定義字段類型轉(zhuǎn)換Handler
這篇文章主要為大家介紹了MyBatisPlus?TypeHandler自定義字段類型轉(zhuǎn)換Handler示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
IDEA報錯java.lang.OutOfMemoryError:Java?heap?space的解決辦法
這篇文章主要給大家介紹了關(guān)于IDEA報錯java.lang.OutOfMemoryError:Java?heap?space的解決辦法,出現(xiàn)這個問題的主要原因是項目運行時的堆內(nèi)存不足引起的報錯,需要的朋友可以參考下2024-02-02
Java實現(xiàn)順時針輸出螺旋二維數(shù)組的方法示例
這篇文章主要介紹了利用Java如何實現(xiàn)順時針輸出螺旋二維數(shù)組的方法示例,文中給出了詳細的示例代碼和注釋,相信對大家具有一定的參考價值,有需要的朋友們下面來一起看看吧。2017-02-02
Java8新特性之JavaFX 8_動力節(jié)點Java學院整理
這篇文章主要介紹了Java8新特性之JavaFX 8的相關(guān)知識,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-06-06
從底層源碼深入分析Spring的IoC容器的實現(xiàn)原理
IoC容器負責管理對象的生命周期和依賴關(guān)系,大大簡化了應用程序的開發(fā)和維,我們這篇文章將會從底層源碼的角度深入分析Spring的IoC容器實現(xiàn),探索它的工作原理和關(guān)鍵組件,需要的朋友可以參考下2023-07-07


