springboot整合短信发送
时间: 2023-11-20 17:56:23 浏览: 100
以下是Spring Boot整合短信发送的步骤和代码示例:
1.在pom.xml文件中添加aliyun-java-sdk-core和aliyun-java-sdk-dysmsapi依赖:
```xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0</version>
</dependency>
```
2.在application.properties文件中添加阿里云短信服务的配置信息:
```properties
aliyun.sms.accessKeyId=yourAccessKeyId
aliyun.sms.accessKeySecret=yourAccessKeySecret
aliyun.sms.templateCode=yourTemplateCode
aliyun.sms.signName=yourSignName
```
3.创建一个SmsUtils工具类,用于发送短信:
```java
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.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
public class SmsUtils {
private static final String PRODUCT = "Dysmsapi";
private static final String DOMAIN = "dysmsapi.aliyuncs.com";
private static final String accessKeyId = "yourAccessKeyId";
private static final String accessKeySecret = "yourAccessKeySecret";
public static SendSmsResponse sendSms(String phoneNumbers, String signName, String templateCode, String templateParam) throws ClientException {
// 设置超时时间-可自行调整
System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000");
// 初始化ascClient需要的几个参数
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);
IAcsClient acsClient = new DefaultAcsClient(profile);
// 组装请求对象
SendSmsRequest request = new SendSmsRequest();
// 必填:待发送手机号
request.setPhoneNumbers(phoneNumbers);
// 必填:短信签名-可在短信控制台中找到
request.setSignName(signName);
// 必填:短信模板-可在短信控制台中找到
request.setTemplateCode(templateCode);
// 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam(templateParam);
// 发送短信
SendSmsResponse response = acsClient.getAcsResponse(request);
return response;
}
}
```
4.在Controller中调用SmsUtils工具类发送短信:
```java
import com.aliyuncs.exceptions.ClientException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@RestController
public class SmsController {
@PostMapping("/sendMsg")
public R<String> sendMsg(@RequestBody User user, HttpSession session) throws ClientException {
// 1.获取手机号
String phone = user.getPhone();
if(StringUtils.isEmpty(phone)){
return R.error("短信发送失败");
}
// 2.随机生成四位验证码
String code = ValidateCodeUtils.generateValidateCode(4).toString();
// 3.调用阿里云提供的短信服务
SmsUtils.sendSms(phone, "yourSignName", "yourTemplateCode", "{\"code\":\"" + code + "\"}");
// 4.需要将生成的验证码保存到session中
session.setAttribute(phone, code);
return R.success("验证码短信发送成功");
}
}
```
阅读全文