Springboot整合阿里巴巴SMS的實(shí)現(xiàn)示例
前提條件
要確保用戶有這個(gè)權(quán)限
還要確保組要有這個(gè)權(quán)限
講反了要先保證組有這個(gè)權(quán)限然后保證用戶有這個(gè)權(quán)限,然后就可以使用這個(gè)用戶的權(quán)限的key來(lái)調(diào)取api了
申請(qǐng)資質(zhì)、簽名等
申請(qǐng)資質(zhì)
點(diǎn)擊這個(gè)進(jìn)入聲請(qǐng)就可以了然后等2個(gè)小時(shí)左右就可以通過(guò)了
申請(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)文章
Spring Boot加密配置文件特殊內(nèi)容的示例代碼詳解
這篇文章主要介紹了Spring Boot加密配置文件特殊內(nèi)容的相關(guān)知識(shí),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05兼容Spring Boot 1.x和2.x配置類參數(shù)綁定的工具類SpringBootBindUtil
今天小編就為大家分享一篇關(guān)于兼容Spring Boot 1.x和2.x配置類參數(shù)綁定的工具類SpringBootBindUtil,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12淺談SpringBoot是如何實(shí)現(xiàn)日志的
這篇文章主要介紹了淺談SpringBoot是如何實(shí)現(xiàn)日志的,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03ThreadPoolExecutor核心線程數(shù)和RocketMQ消費(fèi)線程調(diào)整詳解
這篇文章主要介紹了ThreadPoolExecutor核心線程數(shù)和RocketMQ消費(fèi)線程調(diào)整詳解,Rocketmq 消費(fèi)者在高峰期希望手動(dòng)減少消費(fèi)線程數(shù),通過(guò)DefaultMQPushConsumer.updateCorePoolSize方法可以調(diào)用內(nèi)部的setCorePoolSize設(shè)置多線程核心線程數(shù),需要的朋友可以參考下2023-10-10MybatisPlus實(shí)現(xiàn)insertBatchSomeColumn進(jìn)行批量增加
本文主要介紹了MybatisPlus實(shí)現(xiàn)insertBatchSomeColumn進(jìn)行批量增加,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Java 對(duì)10個(gè)數(shù)進(jìn)行排序的實(shí)現(xiàn)代碼
可以利用選擇法,即從后9個(gè)比較過(guò)程中,選擇一個(gè)最小的與第一個(gè)元素交換, 下次類推,即用第二個(gè)元素與后8個(gè)進(jìn)行比較,并進(jìn)行交換2017-02-02