Springboot整合阿里巴巴SMS的實現(xiàn)示例
前提條件
要確保用戶有這個權(quán)限
還要確保組要有這個權(quán)限
講反了要先保證組有這個權(quán)限然后保證用戶有這個權(quán)限,然后就可以使用這個用戶的權(quán)限的key來調(diào)取api了
申請資質(zhì)、簽名等
申請資質(zhì)
點擊這個進入聲請就可以了然后等2個小時左右就可以通過了
申請簽名
這個是為了之后自定義模板做準(zhǔn)備
添加模板
當(dāng)然第一次是可以注冊釘釘認(rèn)證之后獲取免費的一些額度
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 請參考 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)不一樣但是不這樣寫會報錯,好像是因為這個是直接注入到哪里的,而這里是不需要注入的
// 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的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Springboot整合阿里巴巴SMS內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot加密配置文件特殊內(nèi)容的示例代碼詳解
這篇文章主要介紹了Spring Boot加密配置文件特殊內(nèi)容的相關(guān)知識,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05兼容Spring Boot 1.x和2.x配置類參數(shù)綁定的工具類SpringBootBindUtil
今天小編就為大家分享一篇關(guān)于兼容Spring Boot 1.x和2.x配置類參數(shù)綁定的工具類SpringBootBindUtil,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12ThreadPoolExecutor核心線程數(shù)和RocketMQ消費線程調(diào)整詳解
這篇文章主要介紹了ThreadPoolExecutor核心線程數(shù)和RocketMQ消費線程調(diào)整詳解,Rocketmq 消費者在高峰期希望手動減少消費線程數(shù),通過DefaultMQPushConsumer.updateCorePoolSize方法可以調(diào)用內(nèi)部的setCorePoolSize設(shè)置多線程核心線程數(shù),需要的朋友可以參考下2023-10-10MybatisPlus實現(xiàn)insertBatchSomeColumn進行批量增加
本文主要介紹了MybatisPlus實現(xiàn)insertBatchSomeColumn進行批量增加,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Java 對10個數(shù)進行排序的實現(xiàn)代碼
可以利用選擇法,即從后9個比較過程中,選擇一個最小的與第一個元素交換, 下次類推,即用第二個元素與后8個進行比較,并進行交換2017-02-02