阿里云短信服務(wù)網(wǎng)址:阿里云登錄 - 歡迎登錄阿里云,安全穩(wěn)定的云計(jì)算服務(wù)平臺(tái)
第一步:申請簽名(一般申請時(shí)長在1-2小時(shí)之間)特別注意:場景說明不要亂填以免申請不通過
?第二步:申請短信模板(一般申請時(shí)長在1-2小時(shí)之間)特別注意:場景說明不要亂填以免申請不通過
?第三步:購買短信套餐 | 或者充值大于一條短信的錢數(shù)
?第四步:創(chuàng)建AccessKey (創(chuàng)建完之后記得保存 accessKeyId & accessKeySecret)
?第四步:導(dǎo)入jar包
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-dysmsapi20170525</artifactId>
<version>2.0.22</version>
</dependency>
第五步:封裝發(fā)送驗(yàn)證碼util
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
public class SendSms {
/**
* 發(fā)送驗(yàn)證碼工具類
*
* @param phone
* @return Map<String, String>
*/
public static Map<String, String> sendCode(String phone) {
String accessKeyId = "******";// 填寫自己創(chuàng)建accessKeyId
String accessKeySecret = "*****"; // 填寫自己創(chuàng)建accessKeySecret
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
.accessKeyId(accessKeyId)
.accessKeySecret(accessKeySecret)
.build());
AsyncClient client = AsyncClient.builder()
.region("cn-hangzhou") // Region ID
.credentialsProvider(provider)
.overrideConfiguration(
ClientOverrideConfiguration.create()
.setEndpointOverride("dysmsapi.aliyuncs.com")
)
.build();
SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
.signName("****") // 這里需要填寫自己的簽名名稱
.templateCode("****")// 這里需要填寫自己的短信模板code
.phoneNumbers(phone)
.templateParam("{\"code\":\"7981\"}")//這里是驗(yàn)證碼
.build();
CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
try {
SendSmsResponse resp = response.get();
System.out.println(new Gson().toJson(resp));
} catch (Exception e) {
e.printStackTrace();
} finally {
client.close();
}
Map<String, String> codeMap = new HashMap<>();
codeMap.put(sendSmsRequest.getPhoneNumbers(), sendSmsRequest.getTemplateParam());
return codeMap;
}
}
第六步:編寫Controller接口
/**
* 發(fā)送短信接口
*/
@GetMapping("/code")
public ResponseBean<Map<String, String>> sendCode(@RequestParam("phone") String phone) {
return ResponseBean.success(SendSms.sendCode(phone));
}
測試:
?短信驗(yàn)證碼:
文章來源:http://www.zghlxwxcb.cn/news/detail-735502.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-735502.html
到了這里,關(guān)于最近項(xiàng)目上需要發(fā)送短信整理了一篇文章 SpringBoot整合阿里云發(fā)送短信的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!