> 文章列表 > 【工具篇】Spring Boot 整合阿里云短信-SMS

【工具篇】Spring Boot 整合阿里云短信-SMS

【工具篇】Spring Boot 整合阿里云短信-SMS

短信服务-SMS

短信服务(Short Message Service)是广大企业客户快速触达手机用户所优选使用的通信能力,分为国内短信服务和国际/港澳台短信服务。通过 API/SDK、控制台调用短信发送能力,将指定信息发送至国内或境外手机号码

应用场景

  • 验证码

向手机号码发送包含验证码的短信,支持通过变量替换实现个性短信定制。

通常用于APP、网站的账号注册;登录账户、异地登录时的安全提醒;找回密码时的安全验证;支付认证、身份校验、手机绑定、快捷登录等场景。

  • 短信通知

向手机号码发送通知类短信,支持通过变量替换实现个性短信定制。

通常用于向注册用户下发系统相关信息,包括升级或维护、服务开通、价格调整、 订单确认、物流动态、消费确认、 支付通知等普通通知短信。

  • 推广短信

向手机号码发送包含推广信息的短信,短信内容为经过审核的模板内容,不支持通过变量替换实现个性短信定制。

通常用于向注册用户和潜在客户发送通知和推广信息,包括促销活动通知、业务推广、新产品宣讲、会员关怀等商品与活动的推广信息,可以增加企业产品曝光率、提高产品和企业的知名度。

  • 数字短信

向手机号码发送包含文本、图片、音频、视频的短信,短信内容为经过审核的模板内容。

通常用于影视会员推广、旅游景点介绍、直播电商推广以及各类消费产品的介绍推广等,生动直观的展示,有效吸引目标用户,帮助企业在市场竞争中获取更大的优势。

开通阿里云短信服务

阿里云官方网址:https://www.aliyun.com/

短信服务Java SDK的使用方法及示例

1.进入阿里云短信服务

【工具篇】Spring Boot 整合阿里云短信-SMS

2.点击免费开通

【工具篇】Spring Boot 整合阿里云短信-SMS

【工具篇】Spring Boot 整合阿里云短信-SMS

3.绑定测试手机号码

【工具篇】Spring Boot 整合阿里云短信-SMS

4.测试调用API发送短信

找到调用 API 发短信

记得充点钱在里面,1块钱就可以用很久了,一条短信几分钱。验证码 (0.045元/条);通知短信 (0.045元/条)

【工具篇】Spring Boot 整合阿里云短信-SMS

SpringBoot 整合 阿里云短信

Demo 地址:mingyue-springboot-aliyun-sms

1.添加依赖

<!--  阿里云短信依赖  -->
<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.5.16</version>
</dependency>
<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-dysmsapi</artifactId><version>2.1.0</version>
</dependency>

2.修改配置文件

aliyun:sms:sms-access-key-id: xxxsms-access-key-secret: xxxsms-endpoint: dysmsapi.aliyuncs.comsms-template-code: SMS_154950909sms-sign-name: 阿里云短信测试

3.编写短信推送服务

import cn.hutool.json.JSONUtil;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import java.util.Objects;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;/*** 阿里云短信配置** @author: Strive* @date: 2023/4/16 16:38*/
@Slf4j
@Data
@Service
@ConfigurationProperties("aliyun.sms")
public class AliyunSmsService {private String smsAccessKeyId;private String smsAccessKeySecret;private String smsEndpoint;private String smsSignName;private String smsTemplateCode;/*** 使用 AK&ASK 初始化账号 Client** @param accessKeyId* @param accessKeySecret* @return Client* @throws Exception 短信推送异常*/public static Client createClient(String accessKeyId, String accessKeySecret, String endpoint)throws Exception {Config config =new Config()// 必填,您的 AccessKey ID.setAccessKeyId(accessKeyId)// 必填,您的 AccessKey Secret.setAccessKeySecret(accessKeySecret);// 访问的域名config.endpoint = endpoint;return new com.aliyun.dysmsapi20170525.Client(config);}/*** 发送短信验证码** @param phone 电话号码* @throws Exception 短信推送异常*/public boolean sendCode(String phone, String code) throws Exception {// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的 STSClient client = createClient(this.smsAccessKeyId, this.smsAccessKeySecret, this.smsEndpoint);SendSmsRequest sendSmsRequest =new SendSmsRequest().setSignName(this.smsSignName).setTemplateCode(this.smsTemplateCode).setPhoneNumbers(phone).setTemplateParam("{\\"code\\":\\"" + code + "\\"}");try {log.info("发送短信入参: " + JSONUtil.toJsonStr(sendSmsRequest));// 复制代码运行请自行打印 API 的返回值SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);log.info("发送短信结果: " + JSONUtil.toJsonStr(sendSmsResponse));if (Objects.nonNull(sendSmsResponse) && sendSmsResponse.getBody().code.equals("OK")) {return Boolean.TRUE;}} catch (TeaException error) {// 如有需要,请打印 errorlog.error("短信推送异常结果: " + error.message);return Boolean.FALSE;} catch (Exception e) {TeaException error = new TeaException(e.getMessage(), e);// 如有需要,请打印 errorcom.aliyun.teautil.Common.assertAsString(error.message);log.error("短信推送异常结果: " + error.message);return Boolean.FALSE;}return Boolean.FALSE;}
}

4.编写短信推送接口


/*** 短信推送接口** @author Strive* @date 2023/4/16 10:48*/
@RestController
@RequestMapping("/sms")
@RequiredArgsConstructor
public class SmsController {private final AliyunSmsService smsService;@GetMapping("/send")public ResponseEntity<Boolean> send(String phone) throws Exception {return ResponseEntity.ok(smsService.sendCode(phone, RandomUtil.randomNumbers(4)));}
}

测试发送短信

调用接口:http://127.0.0.1:8080/sms/send