前言
公司最近項目需要一個手機驗證碼的功能,任務確定后,倍感亞歷山大,以為和第三方對接的都好麻煩,查阿里的API、網上大神寫的博客,各種查之后才發現,簡單的一塌糊涂,這里想說個問題,不知道其他的攻城獅們是不是和我一樣的心里,剛接觸個沒做過的任務時,會一臉懵里的著急,無從下手的感覺,后來會了,就覺得簡單的一*,在這里我說一下自己的體會,遇到任何難點,先理思路、任務拆分、逐個查資料,其實一套下來,就不會那種一臉懵逼的干著急。。。
所需條件
1、阿里云賬戶
2、開通云通訊中的短信服務
3、申請短信簽名和模板
4、創建access_key和access_secret
5、然后就是代碼編寫
話不啰嗦,直接開始開發步驟
開發步驟
開通短信服務
創建創建access_key和access_secret
申請短信模板和簽名
開發步驟
1、創建AliyunConfig類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
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層調用
1
2
3
4
5
6
7
8
9
|
/** * @Function: 短信驗證接口 * @author: Yangxf * @Date: 2019/4/11 15:39 */ @RequestMapping ( "/smsverification" ) public Object SmsVerification( @Param ( "phone" ) String phone) { return userViewService.SmsVerification(phone); } |
3、service層代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/** * @Function: 短信驗證 * @author: Yangxf * @Date: 2019/4/11 15:56 * @param: phone 手機號 */ @Override public 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<!-- 阿里云短信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 阿里云短信接口對接內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/qq_37345604/article/details/89214867