關(guān)于springboot集成阿里云短信的問題_第1頁
關(guān)于springboot集成阿里云短信的問題_第2頁
關(guān)于springboot集成阿里云短信的問題_第3頁
關(guān)于springboot集成阿里云短信的問題_第4頁
關(guān)于springboot集成阿里云短信的問題_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第關(guān)于springboot集成阿里云短信的問題目錄1.獲取簽名與模板2.編寫模板與簽名的枚舉類3.配置類4.測試類?

1.獲取簽名與模板

進(jìn)入阿里云平臺(tái),進(jìn)入短信服務(wù)模塊,在以下位置添加簽名和模板(格式一定按照要求填寫審批的比較嚴(yán)格)

2.編寫模板與簽名的枚舉類

在上文獲取模板與簽名成功后,并作為常量放在枚舉類中。

publicenumDySmsEnum{

*本類此處需要修改(短信模板編碼,簽名,參數(shù))

LOGIN_TEMPLATE_CODE("SMS_187570276","自定義軟件框架集成百度云","code");

*短信模板編碼

privateStringtemplateCode;

*簽名

privateStringsignName;

*短信模板必需的數(shù)據(jù)名稱,多個(gè)key以逗號分隔,此處配置作為校驗(yàn)

privateStringkeys;

privateDySmsEnum(StringtemplateCode,StringsignName,Stringkeys){

this.templateCode=templateCode;

this.signName=signName;

this.keys=keys;

publicStringgetTemplateCode(){

returntemplateCode;

publicvoidsetTemplateCode(StringtemplateCode){

this.templateCode=templateCode;

publicStringgetSignName(){

returnsignName;

publicvoidsetSignName(StringsignName){

this.signName=signName;

publicStringgetKeys(){

returnkeys;

publicvoidsetKeys(Stringkeys){

this.keys=keys;

publicstaticDySmsEnumtoEnum(StringtemplateCode){

if(StringUtils.isEmpty(templateCode)){

returnnull;

for(DySmsEnumitem:DySmsEnum.values()){

if(item.getTemplateCode().equals(templateCode)){

returnitem;

returnnull;

3.配置類

這里面需要兩個(gè)參數(shù)accessKeyId,accessKeySecret,需要在以下位置獲取。

importcom.alibaba.fastjson.JSONObject;

importcom.aliyuncs.DefaultAcsClient;

importcom.aliyuncs.IAcsClient;

importcom.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;

importcom.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;

importcom.aliyuncs.exceptions.ClientException;

importfile.DefaultProfile;

importfile.IClientProfile;

importcom.vanpeng.systemportal.modules.dysms.constant.DySmsEnum;

importorg.slf4j.Logger;

importorg.slf4j.LoggerFactory;

importorg.springframework.stereotype.Component;

@Component

publicclassDySmsHelper{

privatefinalstaticLoggerlogger=LoggerFactory.getLogger(DySmsHelper.class);

//產(chǎn)品名稱:云通信短信API產(chǎn)品,無需替換

staticfinalStringproduct="Dysmsapi";

//產(chǎn)品域名,無需替換

staticfinalStringdomain="";

//此處需要替換成開發(fā)者自己的AK(在阿里云訪問控制臺(tái)尋找本類需要修改此處)

staticStringaccessKeyId;

staticStringaccessKeySecret;

publicstaticvoidsetAccessKeyId(StringaccessKeyId){

DySmsHelper.accessKeyId=accessKeyId;

publicstaticvoidsetAccessKeySecret(StringaccessKeySecret){

DySmsHelper.accessKeySecret=accessKeySecret;

publicstaticStringgetAccessKeyId(){

returnaccessKeyId;

publicstaticStringgetAccessKeySecret(){

returnaccessKeySecret;

publicbooleansendSms(Stringphone,JSONObjecttemplateParamJson,DySmsEnumdySmsEnum)throwsClientException{

//可自助調(diào)整超時(shí)時(shí)間

System.setProperty(".client.defaultConnectTimeout","10000");

System.setProperty(".client.defaultReadTimeout","10000");

//初始化acsClient,暫不支持region化

IClientProfileprofile=DefaultProfile.getProfile("cn-hangzhou",accessKeyId,accessKeySecret);

DefaultProfile.addEndpoint("cn-hangzhou","cn-hangzhou",product,domain);

IAcsClientacsClient=newDefaultAcsClient(profile);

//驗(yàn)證json參數(shù)

validateParam(templateParamJson,dySmsEnum);

//組裝請求對象-具體描述見控制臺(tái)-文檔部分內(nèi)容

SendSmsRequestrequest=newSendSmsRequest();

//必填:待發(fā)送手機(jī)號

request.setPhoneNumbers(phone);

//必填:短信簽名-可在短信控制臺(tái)中找到

request.setSignName(dySmsEnum.getSignName());

//必填:短信模板-可在短信控制臺(tái)中找到

request.setTemplateCode(dySmsEnum.getTemplateCode());

//可選:模板中的變量替換JSON串,如模板內(nèi)容為"親愛的${name},您的驗(yàn)證碼為$[code]"時(shí),此處的值為

request.setTemplateParam(templateParamJson.toJSONString());

//選填-上行短信擴(kuò)展碼(無特殊需求用戶請忽略此字段)

//request.setSmsUpExtendCode("90997");

//可選:outId為提供給業(yè)務(wù)方擴(kuò)展字段,最終在短信回執(zhí)消息中將此值帶回給調(diào)用者

//request.setOutId("yourOutId");

booleanresult=false;

//hint此處可能會(huì)拋出異常,注意catch

SendSmsResponsesendSmsResponse=acsClient.getAcsResponse(request);

("短信接口返回的數(shù)據(jù)----------------");

("{Code:"+sendSmsResponse.getCode()+",Message:"+sendSmsResponse.getMessage()+",RequestId:"+sendSmsResponse.getRequestId()+",BizId:"+sendSmsResponse.getBizId()+"}");

if("OK".equals(sendSmsResponse.getCode())){

result=true;

returnresult;

privatestaticvoidvalidateParam(JSONObjecttemplateParamJson,DySmsEnumdySmsEnum){

Stringkeys=dySmsEnum.getKeys();

String[]keyArr=keys.split(",");

for(Stringitem:keyArr){

if(!templateParamJson.containsKey(item)){

thrownewRuntimeException("模板缺少參數(shù):"+item);

4.測試類

使用postman或者其他方式調(diào)用即可發(fā)送短信。

@RestController

@RequestMapping("/SendSmsControllor")

publicclassSendSmsControllor{

@Autowired

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論