如何使用SpringBoot发送短信验证码?

在当今互联网时代,短信验证码已经成为我们生活中不可或缺的一部分。无论是注册账号、登录系统,还是进行支付等操作,都需要验证码来确保操作的安全性。Spring Boot 作为一款流行的 Java 框架,提供了丰富的功能,包括发送短信验证码。本文将详细介绍如何使用 Spring Boot 发送短信验证码。 一、准备工作 1. 环境搭建 首先,确保你的开发环境已经搭建好,包括 Java、Maven、IDE(如 IntelliJ IDEA 或 Eclipse)等。 2. 创建 Spring Boot 项目 使用 Spring Initializr(https://start.spring.io/)创建一个 Spring Boot 项目,选择 Web 模块。 3. 添加依赖 在 `pom.xml` 文件中添加以下依赖: ```xml org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-validation com.aliyun aliyun-java-sdk-core 4.5.3 com.aliyun aliyun-java-sdk-dysmsapi 1.1.0 ``` 二、配置短信发送服务 1. 创建 `SmsService` 接口 在 `src/main/java` 目录下创建 `com.example.demo.service` 包,并在该包下创建 `SmsService` 接口: ```java package com.example.demo.service; public interface SmsService { boolean sendSms(String phone, String templateCode, String signName, String params); } ``` 2. 实现 `SmsService` 接口 在 `src/main/java` 目录下创建 `com.example.demo.service.impl` 包,并在该包下创建 `SmsServiceImpl` 类实现 `SmsService` 接口: ```java package com.example.demo.service.impl; import com.aliyuncs.CommonRequest; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.aliyuncs.profile.DefaultProfile; import com.example.demo.service.SmsService; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.Map; @Service public class SmsServiceImpl implements SmsService { @Value("${aliyun.accessKeyId}") private String accessKeyId; @Value("${aliyun.accessKeySecret}") private String accessKeySecret; @Value("${aliyun.sms.regionId}") private String regionId; @Value("${aliyun.sms.endpoint}") private String endpoint; @Value("${aliyun.sms.templateCode}") private String templateCode; @Value("${aliyun.sms.signName}") private String signName; @Override public boolean sendSms(String phone, String templateCode, String signName, String params) { DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.setMethod("POST"); request.setDomain(endpoint); request.setVersion("2017-05-25"); request.setAction("SendSms"); request.putQueryParameter("PhoneNumbers", phone); request.putQueryParameter("SignName", signName); request.putQueryParameter("TemplateCode", templateCode); request.putQueryParameter("TemplateParam", params); try { SendSmsResponse response = client.doAction(request); return "OK".equals(response.getCode()); } catch (Exception e) { e.printStackTrace(); return false; } } } ``` 3. 配置 `application.properties` 文件 在 `src/main/resources` 目录下创建 `application.properties` 文件,并添加以下配置: ```properties aliyun.accessKeyId=你的AccessKeyId aliyun.accessKeySecret=你的AccessKeySecret aliyun.sms.regionId=你的短信服务区域ID aliyun.sms.endpoint=短信服务接入点 aliyun.sms.templateCode=你的短信模板Code aliyun.sms.signName=你的短信签名 ``` 三、使用短信发送服务 1. 创建 `SmsController` 类 在 `src/main/java` 目录下创建 `com.example.demo.controller` 包,并在该包下创建 `SmsController` 类: ```java package com.example.demo.controller; import com.example.demo.service.SmsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class SmsController { @Autowired private SmsService smsService; @GetMapping("/sendSms") public String sendSms(@RequestParam("phone") String phone, @RequestParam("templateCode") String templateCode, @RequestParam("signName") String signName, @RequestParam("params") String params) { Map map = new HashMap<>(); map.put("code", params); boolean result = smsService.sendSms(phone, templateCode, signName, params); if (result) { return "短信发送成功"; } else { return "短信发送失败"; } } } ``` 2. 启动 Spring Boot 项目 运行 `SmsController` 类所在的 Spring Boot 项目,访问 `http://localhost:8080/sendSms?phone=你的手机号&templateCode=你的模板Code&signName=你的签名¶ms=验证码`,即可发送短信验证码。 四、总结 本文详细介绍了如何使用 Spring Boot 发送短信验证码。通过添加阿里云短信服务依赖、配置短信发送服务、使用短信发送服务,我们可以轻松实现短信验证码的发送。在实际项目中,可以根据需求对短信发送服务进行扩展,如添加短信发送频率限制、验证码有效期等。

猜你喜欢:IM服务