Spring boot项目集成阿里云短信服务发送短信验证码

本文涉及的产品
国际/港澳台短信套餐包,全球plus 100条 6个月
短信服务,200条 3个月
数字短信套餐包(仅限零售电商行业),100条 12个月
简介: Spring boot项目集成阿里云短信服务发送短信验证码

集成阿里云短信服务发送短信

前言

前期准备:需要在阿里云中开通了短信服务并进行相应的配置,可以在我的《阿里云短信服务》中查看系列博客。

系列博客:
一、阿里云 短信服务——发送短信验证码图文教程

二、阿里云 短信服务——开启验证码防盗刷监控

三、阿里云 短信服务——短信发送频率限制
言归正传,本篇博客主要内容是在项目中运用阿里云的短信服务发送短信验证码

业务场景:用户忘记密码,通过发送短信验证码验证用户的真实性

参考bilibili忘记密码的界面理解业务





实现步骤

阿里云官网实现参考地址:SDK地址
在项目不仅要能够实现发送短信验证码,更需要考虑到之后的可复用性、可维护、可扩充性。所以咱们不能仅仅的实现功能。为了之后如果需要发送其他形式的短信,如通知短信、营销短信。以及实现其他阿里云提供的短信服务相关的API如:查询短信发送统计信息、发送卡片短信、批量发送短信等等与短信相关的业务。需要将核心的配置抽离出来,再根据各种业务需求封账相应的实现类。这儿以业务发送短信中的发送短信验证码为例。

1.pom文件引入依赖

<!--阿里云短信服务-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>dysmsapi20170525</artifactId>
            <version>2.0.22</version>
        </dependency>

2.编写阿里云短信服务核心配置类

这块儿用到了nacos做配置管理,accessKeyId,accessKeySecret,endpoint都是从naocs中读取

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
/**
 * @author : [WangWei]
 * @version : [v1.0]
 * @className : ALiYunSMSConfig
 * @description : [阿里云短信服务配置类]
 * @createTime : [2022/11/7 15:39]
 * @updateUser : [WangWei]
 * @updateTime : [2022/11/7 15:39]
 * @updateRemark : [描述说明本次修改内容]
 */
@Configuration
@RefreshScope
public class ALiYunSMSConfig {
    //阿里云账号的accessKeyId
    @Value("${aliyun.sms.accessKeyId}")
    private String accessKeyId;
    //阿里云账号的accessKeySecret
    @Value("${aliyun.sms.accessKeySecret}")
    private String accessKeySecret;
    //短信服务访问的域名
    @Value("${aliyun.sms.endpoint}")
    private String endpoint;
    /*
     * @version V1.0
     * Title: createClient
     * @author Wangwei
     * @description 创建短信服务的代理
     * @createTime  2022/11/7 15:47
     * @param []
     * @return com.aliyun.dysmsapi20170525.Client
     */
    public com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint =endpoint ;
        return new com.aliyun.dysmsapi20170525.Client(config);
    }
}

3.短信服务管理业务实现类SMSConfigServiceImpl

用于实现短信服务相关的方法,目前只是实现了发送短信的方法。

sendShortMessage()实现发送短信,可以通过传入的参数不同,发送不同形式的短信,如短信验证码、通知短信、营销短信。

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teautil.models.RuntimeOptions;
import com.tfjy.arprobackend.config.ALiYunSMSConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * @author : [WangWei]
 * @version : [v1.0]
 * @className : SMSConfigServiceImpl
 * @description : [短信服务管理业务实现类]
 * @createTime : [2022/11/5 17:16]
 * @updateUser : [WangWei]
 * @updateTime : [2022/11/5 17:16]
 * @updateRemark : [描述说明本次修改内容]
 */
@Service
public class SMSConfigServiceImpl{
    private static final Logger log = LogManager.getLogger();
    //阿里云短信服务配置类
    @Autowired
    ALiYunSMSConfig aLiYunSMSConfig;
/*
 * @version V1.0
 * Title: sendShortMessage
 * @author Wangwei
 * @description 发送短信
 * @createTime  2022/11/7 16:02
 * @param [sendSmsRequest]
 * @return com.aliyun.dysmsapi20170525.models.SendSmsResponse
 */
    public SendSmsResponse sendShortMessage(SendSmsRequest sendSmsRequest) throws Exception {
        //初始化配置信息
        Client client=aLiYunSMSConfig.createClient();
        //TODO 配置运行时间选项暂时未进行配置
        RuntimeOptions runtime = new RuntimeOptions();
        SendSmsResponse sendSmsResponse;
        try {
            //发送短信
             sendSmsResponse=client.sendSmsWithOptions(sendSmsRequest, runtime);
        }catch (Exception e){
            throw new Exception("调用阿里云发送短信接口失败",e);
        }
        log.info("调用阿里云发送短信接口成功");
        return sendSmsResponse;
    }
}

4.短信验证码配置类

主要是与发送短信验证码有关的参数配置,也将其放到了nacos中,使用的时候读取nacos中的配置

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
/**
 * @author : [WangWei]
 * @version : [v1.0]
 * @className : SMSConfigModel
 * @description : [短信验证码配置类]
 * @createTime : [2022/11/5 16:06]
 * @updateUser : [WangWei]
 * @updateTime : [2022/11/5 16:06]
 * @updateRemark : [描述说明本次修改内容]
 */
@RefreshScope
@Configuration
public class SMVCodeConfigModel {
    //短信签名名称
    @Value("${aliyun.sms.sMVCode.signName}")
    private String signName;
    //短信模板CODE
    @Value("${aliyun.sms.sMVCode.templateCode}")
    private String templateCode;
    //短信模板变量对应的实际值
    private String templateParam;
    //短信验证码存储在redis中的时间 单位分钟
    @Value("${aliyun.sms.sMVCode.limitTime}")
    private  String limitTime;
    public String getSignName() {
        return signName;
    }
    public void setSignName(String signName) {
        signName = signName;
    }
    public String getTemplateCode() {
        return templateCode;
    }
    public void setTemplateCode(String templateCode) {
        templateCode = templateCode;
    }
    public String getTemplateParam() {
        return templateParam;
    }
    public void setTemplateParam(String templateParam) {
        templateParam = templateParam;
    }
    public String getLimitTime() {
        return limitTime;
    }
    public void setLimitTime(String limitTime) {
        this.limitTime = limitTime;
    }
}

5.发送短信验证码核心代码

      //生成六位手机验证码
            String verificationCode = randomCodeUtils.randomCode();
            //拼接阿里云短信模板变量对应的实际值"{\"code\":\"+verificationCode+\"}";
            HashMap hashMap = new HashMap();
            hashMap.put("code", verificationCode);
            String templateParam = JSON.toJSONString(hashMap);
            //配置发送阿里云短信的请求体
            SendSmsRequest sendSmsRequest=new SendSmsRequest();
            //设置短信签名名称
            sendSmsRequest.setSignName(smvCodeConfigModel.getSignName());
            //设置短信模板Code
            sendSmsRequest.setTemplateCode(smvCodeConfigModel.getTemplateCode());
            //设置发送短信的手机号
            sendSmsRequest.setPhoneNumbers(phoneNumber);
            //设置短信模板变量对应的实际值
            sendSmsRequest.setTemplateParam(templateParam);
            //发送短信响应体
            SendSmsResponse sendSmsResponse;
            try {
                //调用阿里云短信服务发送短信验证码
                sendSmsResponse = smsConfigService.sendShortMessage(sendSmsRequest);
                log.info("调用阿里云短信服务发送短信验证码");
            } catch (Exception e) {
                throw new Exception("调用阿里云短信服务发送短信验证码接口失败!", e);
            }
            if (!sendSmsResponse.getBody().getCode().equals("OK")) {
                log.error("调用阿里云短信服务发送短信验证码失败 {}", sendSmsResponse);
                return false;
            }
            log.info("调用阿里云短信服务发送短信验证码成功 {}", sendSmsResponse);

生成六位随机数的方法

public  String randomCode() {
        StringBuffer  stringBuffer = new StringBuffer ();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            stringBuffer.append(random.nextInt(10));
        }
        return stringBuffer.toString();
    }

实现效果


如果博主的文章对您有所帮助,可以评论、点赞、收藏,支持一下博主!!!

目录
相关文章
|
19天前
|
数据采集 监控 安全
阿里云短信服务+图形认证,有效降低验证码盗刷概率
阿里云短信服务+图形认证服务,有效降低验证码盗刷概率。
阿里云短信服务+图形认证,有效降低验证码盗刷概率
|
2月前
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
定时任务在企业应用中至关重要,常用于异步数据处理、自动化运维等场景。在单体应用中,利用Java的`java.util.Timer`或Spring的`@Scheduled`即可轻松实现。然而,进入微服务架构后,任务可能因多节点并发执行而重复。Spring Cloud Alibaba为此发布了Scheduling模块,提供轻量级、高可用的分布式定时任务解决方案,支持防重复执行、分片运行等功能,并可通过`spring-cloud-starter-alibaba-schedulerx`快速集成。用户可选择基于阿里云SchedulerX托管服务或采用本地开源方案(如ShedLock)
|
2天前
|
存储 前端开发 Java
Spring Boot 集成 MinIO 与 KKFile 实现文件预览功能
本文详细介绍如何在Spring Boot项目中集成MinIO对象存储系统与KKFileView文件预览工具,实现文件上传及在线预览功能。首先搭建MinIO服务器,并在Spring Boot中配置MinIO SDK进行文件管理;接着通过KKFileView提供文件预览服务,最终实现文档管理系统的高效文件处理能力。
|
19天前
|
存储 NoSQL Java
|
2月前
|
数据采集 存储 监控
99%成功率背后:阿里云短信服务有何优势?
为什么短信会发送失败,如何提高短信发送成功率,本文将为您介绍短信发送成功率和阿里云短信服务如何保障企业短信稳定送达等相关知识。
102 1
99%成功率背后:阿里云短信服务有何优势?
|
2月前
|
存储 安全 网络安全
|
2月前
|
测试技术 Java Spring
Spring 框架中的测试之道:揭秘单元测试与集成测试的双重保障,你的应用真的安全了吗?
【8月更文挑战第31天】本文以问答形式深入探讨了Spring框架中的测试策略,包括单元测试与集成测试的有效编写方法,及其对提升代码质量和可靠性的重要性。通过具体示例,展示了如何使用`@MockBean`、`@SpringBootTest`等注解来进行服务和控制器的测试,同时介绍了Spring Boot提供的测试工具,如`@DataJpaTest`,以简化数据库测试流程。合理运用这些测试策略和工具,将助力开发者构建更为稳健的软件系统。
38 0
|
2月前
|
数据库 开发者 Java
颠覆传统开发:Hibernate与Spring Boot的集成,让你的开发效率飞跃式提升!
【8月更文挑战第31天】在 Java 开发中,Spring Boot 和 Hibernate 已成为许多开发者的首选技术栈。Spring Boot 简化了配置和部署过程,而 Hibernate 则是一个强大的 ORM 框架,用于管理数据库交互。将两者结合使用,可以极大提升开发效率并构建高性能的现代 Java 应用。本文将通过代码示例展示如何在 Spring Boot 项目中集成 Hibernate,并实现基本的数据库操作,包括添加依赖、配置数据源、创建实体类和仓库接口,以及在服务层和控制器中处理 HTTP 请求。这种组合不仅简化了配置,还提供了一套强大的工具来快速开发现代 Java 应用程序。
59 0
|
2月前
|
Java Spring
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
|
6天前
|
SQL 监控 druid
springboot-druid数据源的配置方式及配置后台监控-自定义和导入stater(推荐-简单方便使用)两种方式配置druid数据源
这篇文章介绍了如何在Spring Boot项目中配置和监控Druid数据源,包括自定义配置和使用Spring Boot Starter两种方法。
下一篇
无影云桌面