欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

前提條件

用戶權(quán)限

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

組權(quán)限

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

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

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

申請資質(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)文章

  • Lucene源碼系列多值編碼壓縮算法實例詳解

    Lucene源碼系列多值編碼壓縮算法實例詳解

    這篇文章主要為大家介紹了Lucene源碼系列多值編碼壓縮算法實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Spring Boot加密配置文件特殊內(nèi)容的示例代碼詳解

    Spring Boot加密配置文件特殊內(nèi)容的示例代碼詳解

    這篇文章主要介紹了Spring Boot加密配置文件特殊內(nèi)容的相關(guān)知識,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • 兼容Spring Boot 1.x和2.x配置類參數(shù)綁定的工具類SpringBootBindUtil

    兼容Spring Boot 1.x和2.x配置類參數(shù)綁定的工具類SpringBootBindUtil

    今天小編就為大家分享一篇關(guān)于兼容Spring Boot 1.x和2.x配置類參數(shù)綁定的工具類SpringBootBindUtil,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • maven 打包項目的幾種方式

    maven 打包項目的幾種方式

    maven目前在web上面的使用方式很普遍,而打包的方式也存在很多方式,本文就詳細(xì)的介紹了三種方式,具有一定的參考價值,感興趣的可以了解下
    2021-06-06
  • 淺談SpringBoot是如何實現(xiàn)日志的

    淺談SpringBoot是如何實現(xiàn)日志的

    這篇文章主要介紹了淺談SpringBoot是如何實現(xiàn)日志的,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • ThreadPoolExecutor核心線程數(shù)和RocketMQ消費線程調(diào)整詳解

    ThreadPoolExecutor核心線程數(shù)和RocketMQ消費線程調(diào)整詳解

    這篇文章主要介紹了ThreadPoolExecutor核心線程數(shù)和RocketMQ消費線程調(diào)整詳解,Rocketmq 消費者在高峰期希望手動減少消費線程數(shù),通過DefaultMQPushConsumer.updateCorePoolSize方法可以調(diào)用內(nèi)部的setCorePoolSize設(shè)置多線程核心線程數(shù),需要的朋友可以參考下
    2023-10-10
  • MybatisPlus實現(xiàn)insertBatchSomeColumn進行批量增加

    MybatisPlus實現(xiàn)insertBatchSomeColumn進行批量增加

    本文主要介紹了MybatisPlus實現(xiàn)insertBatchSomeColumn進行批量增加,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Java實現(xiàn)猜字小游戲

    Java實現(xiàn)猜字小游戲

    這篇文章給大家分享小編隨手寫的猜字小游戲,基于java代碼寫的,感興趣的朋友跟隨小編一起看看吧
    2019-11-11
  • @RequestBody的使用案例代碼

    @RequestBody的使用案例代碼

    @RequestBody主要用來接收前端傳遞給后端的json字符串中的數(shù)據(jù)的,這篇文章主要介紹了@RequestBody的使用,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-02-02
  • Java 對10個數(shù)進行排序的實現(xiàn)代碼

    Java 對10個數(shù)進行排序的實現(xiàn)代碼

    可以利用選擇法,即從后9個比較過程中,選擇一個最小的與第一個元素交換, 下次類推,即用第二個元素與后8個進行比較,并進行交換
    2017-02-02

最新評論