最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2023年12月14日 10:49:27   作者:墮落年代  
本文主要介紹了Springboot整合阿里巴巴SMS的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前提條件

用戶權(quán)限

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

組權(quán)限

還要確保組要有這個(gè)權(quán)限

講反了要先保證組有這個(gè)權(quán)限然后保證用戶有這個(gè)權(quán)限,然后就可以使用這個(gè)用戶的權(quán)限的key來調(diào)取api了

申請資質(zhì)、簽名等

申請資質(zhì)

申請資質(zhì)

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

申請簽名

在這里插入圖片描述

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

添加模板

添加模板

當(dāng)然第一次是可以注冊釘釘認(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 請參考 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)不一樣但是不這樣寫會報(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • java仿C++模擬實(shí)現(xiàn)一個(gè)智能指針

    java仿C++模擬實(shí)現(xiàn)一個(gè)智能指針

    在?C++?等語言中,指針?是對內(nèi)存地址的直接操作,為靈活管理內(nèi)存和實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)提供了強(qiáng)大能力,本文將使用純Java實(shí)現(xiàn)一個(gè)通用的?SmartPointer<T>框架為各種資源類型提供類似?C++?智能指針的使用體驗(yàn)
    2025-05-05
  • SpringBoot創(chuàng)建maven多模塊項(xiàng)目實(shí)戰(zhàn)代碼

    SpringBoot創(chuàng)建maven多模塊項(xiàng)目實(shí)戰(zhàn)代碼

    本篇文章主要介紹了SpringBoot創(chuàng)建maven多模塊項(xiàng)目實(shí)戰(zhàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • mybatis-plus saveOrUpdateBatch踩坑記錄

    mybatis-plus saveOrUpdateBatch踩坑記錄

    這篇文章主要介紹了mybatis-plus saveOrUpdateBatch踩坑記錄,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Nacos多個(gè)實(shí)例的服務(wù)調(diào)用失敗問題及解決

    Nacos多個(gè)實(shí)例的服務(wù)調(diào)用失敗問題及解決

    在微服務(wù)開發(fā)中,Nacos上出現(xiàn)多個(gè)同一服務(wù)實(shí)例導(dǎo)致OpenFeign調(diào)用失敗,因?yàn)镺penFeign使用SpringCloudLoadbalancer,而SpringCloudLoadbalancer的底層默認(rèn)負(fù)載均衡策略不支持Nacos,通過將負(fù)載均衡策略設(shè)置為Nacos,可以解決調(diào)用失敗的問題
    2026-02-02
  • Springboot實(shí)現(xiàn)公共字段填充的示例詳解

    Springboot實(shí)現(xiàn)公共字段填充的示例詳解

    這篇文章主要為大家詳細(xì)介紹了Springboot實(shí)現(xiàn)公共字段填充的相關(guān)知識,文中的示例代碼講解詳細(xì),對我們深入學(xué)習(xí)springboot有一定的幫助,感興趣的小伙伴可以了解下
    2023-10-10
  • SpringBoot文件上傳與下載功能實(shí)現(xiàn)詳解

    SpringBoot文件上傳與下載功能實(shí)現(xiàn)詳解

    文件上傳與下載是Web應(yīng)用開發(fā)中常用的功能之一。接下來我們將討論如何在Spring?Boot的Web應(yīng)用開發(fā)中,如何實(shí)現(xiàn)文件的上傳與下載,感興趣的可以了解一下
    2022-10-10
  • 最新評論

    福鼎市| 南宫市| 德安县| 德清县| 龙南县| 玛曲县| 阳曲县| 资源县| 新蔡县| 化隆| 晋宁县| 新余市| 玉山县| 化德县| 枣庄市| 丰城市| 湖口县| 夏津县| 建瓯市| 广河县| 互助| 正蓝旗| 饶河县| 咸宁市| 岐山县| 个旧市| 广州市| 濮阳市| 咸宁市| 轮台县| 崇义县| 肇源县| 昂仁县| 宁德市| 科技| 永安市| 茶陵县| 阳朔县| 梅州市| 通化市| 墨江|