SpringBoot使用阿里云实现短信功能

ops/2024/9/23 10:51:16/

引入依赖

<!-- 阿里云依赖 -->
<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.6.3</version>
</dependency>
<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-dysmsapi</artifactId><version>2.2.1</version>
</dependency>

配置文件

# 阿里云短信配置
sms:aliyun:enabled: trueendpoint: cn-hangzhouaccess-key-id: xxxxxxxxaccess-key-secret: xxxxxxxxsign-name: xxxxxxxxtemplate-code: xxxxxxxx

属性类

package com.qiangesoft.sms.config;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;/*** 阿里云短信属性** @author qiangesoft* @date 2024-04-30*/
@Data
@ConfigurationProperties(prefix = "sms.aliyun")
public class AliyunSmsProperties {/*** 是否启用*/private boolean enabled = true;/*** Endpoint*/private String endpoint;/*** accessKeyId*/private String accessKeyId;/*** accessKeySecret*/private String accessKeySecret;/*** 短信签名*/private String signName;/*** 模板code*/private String templateCode;}

配置类

package com.qiangesoft.sms.config;import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 阿里云短信配置** @author qiangesoft* @date 2024-04-30*/
@Configuration
@EnableConfigurationProperties(AliyunSmsProperties.class)
@ConditionalOnProperty(prefix = "sms.aliyun", name = "enabled", havingValue = "true")
public class AliyunSmsConfiguration {private static final String PRODUCT = "Dysmsapi";private static final String DOMAIN = "dysmsapi.aliyuncs.com";@Autowiredprivate AliyunSmsProperties aliyunSmsProperties;@Bean@ConditionalOnMissingBeanpublic IAcsClient acsClient() {String endPoint = aliyunSmsProperties.getEndpoint();String accessKeyId = aliyunSmsProperties.getAccessKeyId();String accessKeySecret = aliyunSmsProperties.getAccessKeySecret();IClientProfile profile = DefaultProfile.getProfile(endPoint, accessKeyId, accessKeySecret);DefaultProfile.addEndpoint(endPoint, PRODUCT, DOMAIN);return new DefaultAcsClient(profile);}}

短信发送实现

package com.qiangesoft.sms.handler;import org.apache.commons.lang3.StringUtils;import java.util.Collection;
import java.util.Collections;
import java.util.Map;/*** 短信处理器** @author qiangesoft* @date 2024-04-30*/
public interface SendHandler {/*** 发送短信** @param phones* @param param* @return*/boolean send(Collection<String> phones, Map<String, Object> param);/*** 发送短信** @param phone* @param param* @return*/default boolean send(String phone, Map<String, Object> param) {if (StringUtils.isBlank(phone)) {return false;}return send(Collections.singletonList(phone), param);}/*** 发送短信** @param phones* @param templateCode* @param param* @return*/boolean send(Collection<String> phones, String templateCode, Map<String, Object> param);/*** 发送短信** @param phone* @param templateCode* @param param* @return*/default boolean send(String phone, String templateCode, Map<String, Object> param) {if (StringUtils.isBlank(phone)) {return false;}return send(Collections.singletonList(phone), param);}
}
package com.qiangesoft.sms.handler;import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.http.MethodType;
import com.qiangesoft.sms.config.AliyunSmsProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;import java.util.Collection;
import java.util.Map;/*** 阿里云短信处理器** @author qiangesoft* @date 2023-05-16*/
@Slf4j
@RequiredArgsConstructor
@Service
public class AliyunSendHandler implements SendHandler {private final IAcsClient acsClient;private final AliyunSmsProperties aliyunSmsProperties;@Overridepublic boolean send(Collection<String> phones, Map<String, Object> param) {return this.send(phones, aliyunSmsProperties.getTemplateCode(), param);}@Overridepublic boolean send(Collection<String> phones, String templateCode, Map<String, Object> param) {SendSmsRequest request = new SendSmsRequest();request.setSysMethod(MethodType.POST);request.setPhoneNumbers(StringUtils.join(phones, ","));request.setSignName(aliyunSmsProperties.getSignName());request.setTemplateCode(aliyunSmsProperties.getTemplateCode());request.setTemplateParam(JSONObject.toJSONString(param));try {SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);if ("OK".equals(sendSmsResponse.getCode())) {return true;}log.debug("send fail[code={}, message={}]", sendSmsResponse.getCode(), sendSmsResponse.getMessage());} catch (Exception e) {log.debug(e.getMessage(), e);}return false;}}

测试

package com.qiangesoft.sms.controller;import com.qiangesoft.sms.handler.SendHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;/*** 短信测试** @author qiangesoft* @date 2024-04-30*/
@RequestMapping("/sms")
@RestController
public class SmsController {@Autowiredprivate SendHandler sendHandler;@GetMapping("/send")public Boolean send() {Map<String, Object> param = new HashMap<>();param.put("name", "张三");param.put("jds", 111);param.put("adds", "顶顶顶帆帆");return sendHandler.send("15222222222", param);}}

http://www.ppmy.cn/ops/26085.html

相关文章

基于RK1126的小型化低功耗AI相机,支持人体特征识别、人脸特征识别、案例帽识别等

提供可定制的⼀套 AI相机软硬件开发平台&#xff0c; 硬件采⽤ RockchipRV1126处理器解决 ⽅案&#xff0c;可选择搭配 SonyIMX系列传感器&#xff0c;POE供电与数据传输&#xff0c;采⽤ 38板标准结构设计&#xff0c;快速按需定制外壳&#xff0c;⽀撑从开发到验证到批量⽣产…

第9篇:创建Nios II工程之读取Switch的值<二>

Q&#xff1a;上一期我们完成了Quartus硬件工程部分&#xff0c;本期我们创建Nios II软件工程这部分。 A&#xff1a;创建完BSP和Nios II Application之后&#xff0c;在source文件main.c中添加代码&#xff1a;system.h头文件中新增了Switch PIO IP的硬件信息&#xff0c;包括…

深入探索MinIO:高性能的开源对象存储服务器

深入探索MinIO&#xff1a;高性能的开源对象存储服务器 文章目录 深入探索MinIO&#xff1a;高性能的开源对象存储服务器一、引言二、MinIO概述三、MinIO的特点四、MinIO的架构五、MinIO的使用场景六、MinIO的安装部署七、MinIO的操作管理八、MinIO的生态系统九、MinIO的扩展性…

Linux的vim下制作进度条

目录 前言&#xff1a; 回车和换行有区别吗&#xff1f; 回车和换行的区别展示&#xff08;这个我在Linux下演示&#xff09; 为什么会消失呢? 回车和换行的区别 为什么\r和\n产生的效果不同&#xff1f; 打印进度条&#xff1a; &#xff08;1&#xff09;打印字符串 …

不同规模的态势感知如何组合、切换

不同规模的态势感知可以通过组合和切换来实现更全面、高效的信息获取和分析。 一种组合方式是将各个规模的态势感知系统连接起来&#xff0c;形成一个层级结构。较小规模的感知系统可以负责实时收集和处理局部区域的信息&#xff0c;较大规模的感知系统则可以负责整体区域的信息…

chrome和drive安装包路径

Chrome for Testing availability (googlechromelabs.github.io) 下载Stable下面的包哈

聚醚醚酮(Polyether Ether Ketone)PEEK在粘接使用时可以使用UV胶水吗?要注意哪些事项?

一般情况下&#xff0c;聚醚醚酮&#xff08;Polyether Ether Ketone&#xff0c;PEEK&#xff09;是一种难以黏附的高性能工程塑料&#xff0c;而UV胶水通常不是与PEEK进行粘接的首选方法。PEEK表面的化学性质和高温性能使得它对常规胶水的附着性较低。然而&#xff0c;有一些…

k8s中,configMap与环境变量的关系

总结 在Kubernetes中&#xff0c;ConfigMap和环境变量都可以用于向容器传递配置信息。以下是它们的区别&#xff1a; 环境变量&#xff1a;这是一种将配置信息直接注入到容器中的方法。它们在容器启动时被设置&#xff0c;并且在容器的生命周期内保持不变。ConfigMap&#xf…