亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

SpringBoot實現阿里云短信接口對接的示例代碼

瀏覽:8日期:2023-04-20 13:29:49

前言

公司最近項目需要一個手機驗證碼的功能,任務確定后,倍感亞歷山大,以為和第三方對接的都好麻煩,查阿里的API、網上大神寫的博客,各種查之后才發現,簡單的一塌糊涂,這里想說個問題,不知道其他的攻城獅們是不是和我一樣的心里,剛接觸個沒做過的任務時,會一臉懵里的著急,無從下手的感覺,后來會了,就覺得簡單的一*,在這里我說一下自己的體會,遇到任何難點,先理思路、任務拆分、逐個查資料,其實一套下來,就不會那種一臉懵逼的干著急。。。

所需條件

1、阿里云賬戶

2、開通云通訊中的短信服務

3、申請短信簽名和模板

4、創建access_key和access_secret

5、然后就是代碼編寫

話不??攏?苯涌?伎?⒉街?/p>

開發步驟

開通短信服務

SpringBoot實現阿里云短信接口對接的示例代碼

創建創建access_key和access_secret

SpringBoot實現阿里云短信接口對接的示例代碼

申請短信模板和簽名

SpringBoot實現阿里云短信接口對接的示例代碼

開發步驟

1、創建AliyunConfig類

package com.preread.user.config;import com.aliyuncs.DefaultAcsClient;import com.aliyuncs.IAcsClient;import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;import com.aliyuncs.exceptions.ClientException;import com.aliyuncs.profile.DefaultProfile;import com.aliyuncs.profile.IClientProfile;import java.util.Random;/** * @Description: 阿里云短信接口配置類 * @author: yangxf * @date: 2019/4/11 15:01 */public class AliyunConfig { /* 短信API產品名稱(短信產品名固定,無需修改) */ private static final String product = 'Dysmsapi'; /* 短信API產品域名,接口地址固定,無需修改 */ private static final String domain = 'dysmsapi.aliyuncs.com'; /* 此處需要替換成開發者自己的accessKeyId和accessKeySecret(在阿里云訪問控制臺尋找) */ private static final String accessKeyId = '你的accessKeyId'; //TODO: 這里要寫成你自己生成的 private static final String accessKeySecret = '你的accessKeySecret';//TODO: 這里要寫成你自己生成的 /* 短信發送 */ public static SendSmsResponse sendSms(String phone) throws ClientException { /* 超時時間,可自主調整 */ System.setProperty('sun.net.client.defaultConnectTimeout', '10000'); System.setProperty('sun.net.client.defaultReadTimeout', '10000'); /* 初始化acsClient,暫不支持region化 */ IClientProfile profile = DefaultProfile.getProfile('cn-hangzhou', accessKeyId, accessKeySecret); DefaultProfile.addEndpoint('cn-hangzhou', 'cn-hangzhou', product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); /* 組裝請求對象-具體描述見控制臺-文檔部分內容 */ SendSmsRequest request = new SendSmsRequest(); /* 必填:待發送手機號 */ request.setPhoneNumbers(phone); /* 必填:短信簽名-可在短信控制臺中找到 */ request.setSignName('提前看'); //TODO: 這里是你短信簽名 /* 必填:短信模板code-可在短信控制臺中找到 */ request.setTemplateCode('你的模板code'); //TODO: 這里是你的模板code /* 可選:模板中的變量替換JSON串,如模板內容為'親愛的用戶,您的驗證碼為$[code]'時,此處的值為 */ request.setTemplateParam('{'code':'' + getMsgCode() + ''}'); // hint 此處可能會拋出異常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals('OK')){ System.out.println('短信發送成功!驗證碼:' + getMsgCode()); }else { System.out.println('短信發送失敗!'); } return sendSmsResponse; } /** * @Function: 生成驗證碼 * @author: yangxf * @Date: 2019/4/11 15:30 */ private static String getMsgCode() { int n = 6; StringBuilder code = new StringBuilder(); Random ran = new Random(); for (int i = 0; i < n; i++) { code.append(Integer.valueOf(ran.nextInt(10)).toString()); } return code.toString(); }}

2、controller層調用

/** * @Function: 短信驗證接口 * @author: Yangxf * @Date: 2019/4/11 15:39 */ @RequestMapping('/smsverification') public Object SmsVerification(@Param('phone') String phone) { return userViewService.SmsVerification(phone); }

3、service層代碼

/** * @Function: 短信驗證 * @author: Yangxf * @Date: 2019/4/11 15:56 * @param: phone 手機號 */@Overridepublic Map<String, Object> SmsVerification(String phone) { Map<String, Object> map = new HashMap<>(); try { AliyunConfig.sendSms(phone); map.put('code', 200); map.put('msg', '短信驗證發送成功'); return map; } catch (ClientException e) { map.put('code', 300); map.put('msg', e.getMessage()); return map; }}

4、集成阿里云SDK

<!-- 阿里云短信SDK --><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.1.0</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-dysmsapi</artifactId><version>1.1.0</version></dependency><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId></dependency><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.7</version></dependency>

至此代碼階段OK,可以測試了

SpringBoot實現阿里云短信接口對接的示例代碼

效果如下:

SpringBoot實現阿里云短信接口對接的示例代碼

到此這篇關于SpringBoot實現阿里云短信接口對接的示例代碼的文章就介紹到這了,更多相關SpringBoot 阿里云短信接口對接內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 中文字幕在线影院 | 超级碰碰碰碰97久久久久 | 欧美极品福利视频在线播放 | 日日夜操| 欧美久久久久欧美一区 | 中国一级全黄的免费观看 | 成人最新午夜免费视频 | 日韩 欧美 国产 亚洲 中文 | 91国自产精品中文字幕亚洲 | 久久一区二区免费播放 | 上海毛片 | 日韩在线观 | 99在线精品日韩一区免费国产 | 中文字幕亚洲欧美一区 | 国产90后美女露脸在线观看 | 国产美女精品 | 一级女性大黄生活片免费 | 九九精品视频在线 | 精品国产一区二区三区香蕉沈先生 | 亚洲欧美日韩综合在线播放 | 新黄色网址 | 免费成人毛片 | 六月婷婷在线 | 亚洲黄色美女视频 | 欧美伦理三级在线播放影院 | 欧美精品v国产精品v日韩精品 | 免费观看一级特黄欧美大片 | 91粉色视频在线观看 | 黄色免费一级 | 青青久久久 | 精品国产第一国产综合精品 | 你操综合| 免费看一级欧美毛片视频 | 久久99精品久久久久子伦小说 | 久久精品国产半推半就 | 浮荡视频在线观看免费 | 亚洲美女色成人综合 | 国产精品无卡无在线播放 | 国产香港特级一级毛片 | 亚洲精品专区一区二区欧美 | 永久免费观看午夜视频在线 |