Springboot整合阿里巴巴SMS的實(shí)現(xiàn)示例
前提條件

要確保用戶有這個(gè)權(quán)限

還要確保組要有這個(gè)權(quán)限
講反了要先保證組有這個(gè)權(quán)限然后保證用戶有這個(gè)權(quán)限,然后就可以使用這個(gè)用戶的權(quán)限的key來調(diào)取api了
申請(qǐng)資質(zhì)、簽名等
申請(qǐng)資質(zhì)

點(diǎn)擊這個(gè)進(jìn)入聲請(qǐng)就可以了然后等2個(gè)小時(shí)左右就可以通過了
申請(qǐng)簽名

這個(gè)是為了之后自定義模板做準(zhǔn)備
添加模板

當(dāng)然第一次是可以注冊(cè)釘釘認(rèn)證之后獲取免費(fèi)的一些額度
api引入
依賴引入
<!--sms的服務(wù)-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-dysmsapi20170525</artifactId>
<version>2.0.24</version>
</dependency>
代碼部分
package com.example.lpms.tool;
import com.example.lpms.common.R;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import com.aliyun.sdk.service.dysmsapi20170525.*;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import java.util.concurrent.CompletableFuture;
/**
* @author:DUOLUONIANDAI
* @DATA:2023/12/13
* @Title:
*/
@Component
public class SMSTool {
@Value("${spring.sms.id}")
String id;
@Value("${spring.sms.secret}")
String secret;
@Value("${spring.sms.sign-name}")
String signName;
@Value("${spring.sms.templateCode}")
String templateCode;
public R sendSMS(String phone, String captcha) {
try {
// Configure Credentials authentication information, including ak, secret, token
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
.accessKeyId(id)
.accessKeySecret(secret)
//.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
.build());
// Configure the Client
AsyncClient client = AsyncClient.builder()
.region("cn-shanghai") // Region ID
//.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
.credentialsProvider(provider)
//.serviceConfiguration(Configuration.create()) // Service-level configuration
// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
.overrideConfiguration(
ClientOverrideConfiguration.create()
// Endpoint 請(qǐng)參考 https://api.aliyun.com/product/Dysmsapi
.setEndpointOverride("dysmsapi.aliyuncs.com")
//.setConnectTimeout(Duration.ofSeconds(30))
)
.build();
// Parameter settings for API request
SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
.signName(signName)
.templateCode(templateCode)
.phoneNumbers(phone)
.templateParam("{\"code\":\"" + captcha + "\"}")
// Request-level configuration rewrite, can set Http request parameters, etc.
// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
.build();
// Asynchronously get the return value of the API request
CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
// Synchronously get the return value of the API request
SendSmsResponse resp = response.get();
System.out.println(new Gson().toJson(resp));
// Finally, close the client
client.close();
} catch (Exception e) {
e.printStackTrace();
return R.fail();
}
return R.ok();
}
}
注意
這下面和官網(wǎng)不一樣但是不這樣寫會(huì)報(bào)錯(cuò),好像是因?yàn)檫@個(gè)是直接注入到哪里的,而這里是不需要注入的
// Configure Credentials authentication information, including ak, secret, token
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
.accessKeyId(id)
.accessKeySecret(secret)
//.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
.build());
到此這篇關(guān)于Springboot整合阿里巴巴SMS的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Springboot整合阿里巴巴SMS內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
自定義一個(gè)簡(jiǎn)單的JDBC連接池實(shí)現(xiàn)方法
下面小編就為大家分享一篇自定義一個(gè)簡(jiǎn)單的JDBC連接池實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
java仿C++模擬實(shí)現(xiàn)一個(gè)智能指針
SpringBoot創(chuàng)建maven多模塊項(xiàng)目實(shí)戰(zhàn)代碼
mybatis-plus saveOrUpdateBatch踩坑記錄
Nacos多個(gè)實(shí)例的服務(wù)調(diào)用失敗問題及解決
Springboot實(shí)現(xiàn)公共字段填充的示例詳解
SpringBoot文件上傳與下載功能實(shí)現(xiàn)詳解

