詳細介紹Java阿里云的短信驗證碼實現(xiàn)
阿里云手機短信驗證碼
第一步 登錄阿里云開放平臺
1、進入阿里云開放平臺---->點擊控制臺
2、點擊AccessKey管理
3、點擊之后會彈出提示,選擇開始使用子用戶
4、新建一個用戶組,然后按要求填寫即可
5、創(chuàng)建一個用戶,按要求填寫內(nèi)容,勾選編程訪問
6、會得到AccessKey(id,密碼)
要將這個賬號保存下來!
//用戶登錄名稱 ==================== //AccessKey ID ==================== //AccessKey Secret ============================
第二步,開通阿里云短信服務(wù)
1、找到短信控制臺面板,點擊國內(nèi)消息
2、選擇模板管理,點擊添加模板
以下內(nèi)容按要求填寫即可,申請說明需要有正當理由不然審核可能通不過
點擊提交,等待審核
3、步驟同上,點擊簽名管理,添加簽名
注:簽名需要填寫應(yīng)用名稱、網(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,設(shè)置失效時間 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é)語:
可以編寫可復(fù)用的微服務(wù)接口,來實現(xiàn)驗證碼的發(fā)送,根據(jù)實際情況進行封裝
到此這篇關(guān)于詳細介紹Java阿里云的短信驗證碼實現(xiàn)的文章就介紹到這了,更多相關(guān)Java阿里云短信驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Java并發(fā)工具類之CountDownLatch和CyclicBarrier
在JDK的并發(fā)包中,有幾個非常有用的并發(fā)工具類,它們分別是:CountDownLatch、CyclicBarrier、Semaphore和Exchanger,本文主要來講講其中CountDownLatch和CyclicBarrier的使用,感興趣的可以了解一下2023-06-06java servlet手機app訪問接口(三)高德地圖云存儲及檢索
這篇文章主要為大家詳細介紹了java servlet手機app訪問接口(三),高德地圖云存儲及檢索,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12Java深入學習圖形用戶界面GUI之創(chuàng)建窗體
圖形編程中,窗口是一個重要的概念,窗口其實是一個矩形框,應(yīng)用程序可以使用其從而達到輸出結(jié)果和接受用戶輸入的效果,學習了GUI就讓我們用它來創(chuàng)建一個窗體2022-05-05淺談synchronized方法對非synchronized方法的影響
下面小編就為大家?guī)硪黄獪\談synchronized方法對非synchronized方法的影響。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10實例解析Json反序列化之ObjectMapper(自定義實現(xiàn)反序列化方法)
這篇文章主要介紹了實例解析Json反序列化之ObjectMapper,json自定義序列化的方法,需要的朋友可以了解下。2017-09-09Spring Boot 整合 Shiro+Thymeleaf過程解析
這篇文章主要介紹了Spring Boot 整合 Shiro+Thymeleaf過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-10-10