詳細介紹Java阿里雲的簡訊驗證碼實現
阿新 • • 發佈:2020-06-02
阿里雲手機簡訊驗證碼
第一步 登入阿里雲開放平臺
1、進入阿里雲開放平臺---->點選控制檯
2、點選AccessKey管理
3、點選之後會彈出提示,選擇開始使用子使用者
4、新建一個使用者組,然後按要求填寫即可
5、建立一個使用者,按要求填寫內容,勾選程式設計訪問
6、會得到AccessKey(id,密碼)
要將這個賬號儲存下來!
//使用者登入名稱 ==================== //AccessKey ID ==================== //AccessKey Secret ============================
第二步,開通阿里雲簡訊服務
1、找到簡訊控制檯面板,點選國內訊息
2、選擇模板管理,點選新增模板
以下內容按要求填寫即可,申請說明需要有正當理由不然稽核可能通不過
點選提交,等待稽核
3、步驟同上,點選簽名管理,添加簽名
注:簽名需要填寫應用名稱、網站名稱 例如:(dy學習網站) 個人使用者選擇適用場景的時候選擇驗證碼,申請說明需要填寫正當理由。不然可能稽核不通過。
提交後等待稽核通過即可。
第三步,編寫程式碼測試
1、新建專案
建立一個Java專案,筆者建立的是SpringBoot專案
2、新增依賴
點選快速學習——> 檢視APIDemo,裡面會有maven的依賴和官方的Demo
maven依賴:
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.0</version> </dependency>
3、編寫程式碼
簡單測試,結合了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); //呼叫service層的方法 傳兩個引數:phone,map Boolean flag = sendMessage.sendMessage(phone,map); if (flag){ //存入redis,設定失效時間 template.opsForValue().set(phone,code,5,TimeUnit.MINUTES); return phone+":"+"驗證碼"+code+"傳送成功!"; } return "傳送失敗"; } }
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)); //連線阿里雲,第一個引數不用改變,後兩個填寫自己的accessKeyId和accessSecret DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou","<accessKeyId>","<accessSecret>"); IAcsClient client = new DefaultAcsClient(profile); //構建請求! CommonRequest request = new CommonRequest(); request.setSysMethod(MethodType.POST); request.setSysDomain("dysmsapi.aliyuncs.com"); //不要動 request.setSysVersion("2017-05-25"); //不要動 request.setSysAction("SendSms"); //自定義引數(手機號,驗證碼,簽名,模板) request.putQueryParameter("RegionId","cn-hangzhou"); request.putQueryParameter("PhoneNumbers",phoneNum); request.putQueryParameter("SignName","簽名"); request.putQueryParameter("TemplateCode","模板(SMS-*****)"); //構建一個簡訊的驗證碼 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:
簡訊:
結語:
可以編寫可複用的微服務介面,來實現驗證碼的傳送,根據實際情況進行封裝
到此這篇關於詳細介紹Java阿里雲的簡訊驗證碼實現的文章就介紹到這了,更多相關Java阿里雲簡訊驗證碼內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!