Springboot整合阿里巴巴SMS的实现示例

 更新时间:2023年12月14日 10:49:27   作者:堕落年代  
本文主要介绍了Springboot整合阿里巴巴SMS的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前提条件

用户权限

要确保用户有这个权限

组权限

还要确保组要有这个权限

讲反了要先保证组有这个权限然后保证用户有这个权限,然后就可以使用这个用户的权限的key来调取api了

申请资质、签名等

申请资质

申请资质

点击这个进入声请就可以了然后等2个小时左右就可以通过了

申请签名

在这里插入图片描述

这个是为了之后自定义模板做准备

添加模板

添加模板

当然第一次是可以注册钉钉认证之后获取免费的一些额度

api引入

依赖引入

<!--sms的服务-->
 <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();
    }

}

注意

这下面和官网不一样但是不这样写会报错,好像是因为这个是直接注入到哪里的,而这里是不需要注入的

// 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());

到此这篇关于Springboot整合阿里巴巴SMS的实现示例的文章就介绍到这了,更多相关Springboot整合阿里巴巴SMS内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • Lucene源码系列多值编码压缩算法实例详解

    Lucene源码系列多值编码压缩算法实例详解

    这篇文章主要为大家介绍了Lucene源码系列多值编码压缩算法实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • Spring Boot加密配置文件特殊内容的示例代码详解

    Spring Boot加密配置文件特殊内容的示例代码详解

    这篇文章主要介绍了Spring Boot加密配置文件特殊内容的相关知识,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05
  • 兼容Spring Boot 1.x和2.x配置类参数绑定的工具类SpringBootBindUtil

    兼容Spring Boot 1.x和2.x配置类参数绑定的工具类SpringBootBindUtil

    今天小编就为大家分享一篇关于兼容Spring Boot 1.x和2.x配置类参数绑定的工具类SpringBootBindUtil,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • maven 打包项目的几种方式

    maven 打包项目的几种方式

    maven目前在web上面的使用方式很普遍,而打包的方式也存在很多方式,本文就详细的介绍了三种方式,具有一定的参考价值,感兴趣的可以了解下
    2021-06-06
  • 浅谈SpringBoot是如何实现日志的

    浅谈SpringBoot是如何实现日志的

    这篇文章主要介绍了浅谈SpringBoot是如何实现日志的,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • ThreadPoolExecutor核心线程数和RocketMQ消费线程调整详解

    ThreadPoolExecutor核心线程数和RocketMQ消费线程调整详解

    这篇文章主要介绍了ThreadPoolExecutor核心线程数和RocketMQ消费线程调整详解,Rocketmq 消费者在高峰期希望手动减少消费线程数,通过DefaultMQPushConsumer.updateCorePoolSize方法可以调用内部的setCorePoolSize设置多线程核心线程数,需要的朋友可以参考下
    2023-10-10
  • MybatisPlus实现insertBatchSomeColumn进行批量增加

    MybatisPlus实现insertBatchSomeColumn进行批量增加

    本文主要介绍了MybatisPlus实现insertBatchSomeColumn进行批量增加,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • Java实现猜字小游戏

    Java实现猜字小游戏

    这篇文章给大家分享小编随手写的猜字小游戏,基于java代码写的,感兴趣的朋友跟随小编一起看看吧
    2019-11-11
  • @RequestBody的使用案例代码

    @RequestBody的使用案例代码

    @RequestBody主要用来接收前端传递给后端的json字符串中的数据的,这篇文章主要介绍了@RequestBody的使用,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-02-02
  • Java 对10个数进行排序的实现代码

    Java 对10个数进行排序的实现代码

    可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换, 下次类推,即用第二个元素与后8个进行比较,并进行交换
    2017-02-02

最新评论