線上系統開發中,簡訊功能是經常要用到的,比如註冊、修改手機號、修改密碼時簡訊驗證碼等。我們這裡是一個基於Springboot的微服務(SpringCloud Alibaba)專案,選擇阿里雲的簡訊介面。
登入阿里雲簡訊控制檯,瞭解簡訊相關知識。我們這裡需要簡訊傳送功能,進一步瞭解相關API。
簽名和模板是阿里雲簡訊功能所必須的,下面講解下簽名和模板的新增。
新增模板:
稽核不通過原因:
效果圖示:
雖然是做了前面的準備工作,但是具體怎麼應用還是很模糊,查閱相關技術檔案,很多都是舊版本的內容。這裡我們還是通過阿里雲的OpenAPI來學習最新的應用技術,這裡我們以簡訊傳送為例,圖示:
api引數,範例,依賴一目瞭然,而且是最新版本的內容,下面我們開始整合到專案中。
pom.xml:複製上面依賴資訊
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.21</version>
</dependency>
分析:
參考官網給出的SDK封裝我們自己的AliSms類,原始碼:
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import cn.hutool.core.bean.BeanUtil;
import java.util.Map;
/**
* @author Administrator
* @version 1.0
* @description ali sms
* @date 2022-09-30 11:19
* 阿里雲簡訊類
*/
public class AliSms {
private final Client client;
private final SendSmsRequest request;
public AliSms(Client client, SendSmsRequest request) {
this.client = client;
this.request = request;
}
public Map<String, Object> sendSms(String templateCode, String templateParam, String phoneNumbers) throws Exception {
request.setTemplateCode(templateCode);
request.setTemplateParam(templateParam);
request.setPhoneNumbers(phoneNumbers);
RuntimeOptions runtime = new RuntimeOptions();
SendSmsResponse response = null;
try {
response = client.sendSmsWithOptions(request, runtime);
} catch (Exception e) {
e.printStackTrace();
throw new Exception("簡訊傳送失敗");
}
return BeanUtil.beanToMap(response);
}
}
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Administrator
* @version 1.0
* @description sms簡訊傳送
* @date 2022-10-04 12:50
*/
@Configuration
public class SmsAutoConfiguration {
/**
* 阿里雲簡訊服務賬戶accessKey
*/
@Value("${spring.cloud.alicloud.access-key}")
private String accessKey;
/**
* 阿里雲簡訊服務賬戶accessKey
*/
@Value("${spring.cloud.alicloud.secret-key}")
private String secretKey;
/**
* 阿里雲簡訊服務endpoint
*/
@Value("${spring.cloud.alicloud.sms.endpoint}")
private String endpoint;
/**
* 阿里雲簡訊服務簽名
*/
@Value("${spring.cloud.alicloud.sms.signName}")
private String signName;
@Bean
public AliSms aliSms() {
return new AliSms(createClient(), sendSmsRequest());
}
private SendSmsRequest sendSmsRequest() {
SendSmsRequest request = new SendSmsRequest();
request.setSignName(signName);
return request;
}
private Client createClient(){
Config config = new Config()
// 您的 AccessKey ID
.setAccessKeyId(accessKey)
// 您的 AccessKey Secret
.setAccessKeySecret(secretKey);
// 存取的域名
config.endpoint = endpoint;
Client client = null;
try {
client = new Client(config);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("建立阿里使用者端失敗!");
}
return client;
}
}
pom.xml新增依賴,全部相關依賴:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.21</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea-util</artifactId>
<version>0.2.14</version>
</dependency>
@Autowired
private AliSms aliSms;
@Override
public void sendSms(Sms sms) {
try {
log.info("傳送簡訊{}", JSON.toJSONString(sms, true));
String templateParam = "{\"code\":\"" + "123456" + "\"}";
Map<String, Object> info = aliSms.sendSms(sms.getTemplateCode(), templateParam, sms.getMobile());
log.info("傳送結果:{}", JSON.toJSONString(info, true));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("傳送簡訊失敗");
}
}
測試結果:
控制檯:
{
"basePath":"http://192.168.10.1:8090",
"description":"傳送簡訊",
"ip":"192.168.10.1",
"method":"com.gaogzhen.controller.SmsController.sendSms",
"parameter":{
"sms":{
"countryCode":"+86",
"mobile":"自己填寫的手機號",
"templateCode":"自己的模板CODE"
}
},
"result":{
"code":200
},
"spendTime":0,
"uri":"/sms/sendTo",
"url":"http://192.168.10.1:8090/sms/sendTo",
"username":"1014066909280374785"
}
手機截圖:
歡迎交流學習,下面為聯絡方式和倉庫原始碼地址
❓QQ:806797785