一.开通阿里云短信服务
- 开通短信服务
进入阿里云,搜索短信服务,点击免费开通,发送短信的服务是收费的,可以冲个1块钱
- 短信服务相关配置
主要看着两个配置
- 国内消息设置:配置一些安全性的东西,避免被盗刷
- 国内消息:配置API调用时需要的签名和模板
按照步骤申请签名和模板即可
二.API操作
- 引入Maven依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
- 编写业务代码,这里引入了Redis来做1分钟超时,每个参数都给出了注释
package com.xx.service.impl;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.xx.exception.MyException;
import com.xx.service.MsmService;
import com.xx.utils.ParamUtil;
import com.xx.utils.RandomUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
/**
* @author aqi
* DateTime: 2020/11/9 3:47 下午
* Description: No Description
*/
@Service
public class MsmServiceImpl implements MsmService {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Override
public void sendMessages(String phone) {
// 每分钟只允许发一次
String phoneIsEmpty = stringRedisTemplate.opsForValue().get(phone);
// 如果redis中还存在这个手机号,则不允许发送短信
if (!StringUtils.isEmpty(phoneIsEmpty)) {
throw new MyException("短信已发送");
}
DefaultProfile profile = DefaultProfile.getProfile("default", ParamUtil.KEY_ID, ParamUtil.KEY_SECRET);
IAcsClient client = new DefaultAcsClient(profile);
// 设置相关参数
CommonRequest request = new CommonRequest();
// 请求方式
request.setSysMethod(MethodType.POST);
// 短信API产品域名,固定写法
request.setSysDomain(ParamUtil.DOMAIN);
// API的版本号,固定写法
request.setSysVersion("2017-05-25");
// 指定要调用的API,固定写法
request.setSysAction("SendSms");
// 设置手机号
request.putQueryParameter("PhoneNumbers", phone);
// 设置签名名称
request.putQueryParameter("SignName", ParamUtil.SIGN_NAME);
// 设置模板CODE
request.putQueryParameter("TemplateCode", ParamUtil.TEMPLATE_CODE);
// 设置验证码,这里的验证码需要是JSON形式(固定格式)
String code = RandomUtil.getFourBitRandom();
request.putQueryParameter("TemplateParam", code);
try {
// 发送短信
client.getCommonResponse(request);
stringRedisTemplate.opsForValue().set(phone, code, 1, TimeUnit.MINUTES);
} catch (ClientException e) {
e.printStackTrace();
}
}
}
四位或者六位随机验证码生成工具类
package com.xx.utils;
import com.alibaba.fastjson.JSONObject;
import java.text.DecimalFormat;
import java.util.*;
/**
* @author aqi
* DateTime: 2020/11/10 9:28 上午
* Description: 短信验证码
*/
public class RandomUtil {
private static final Random random = new Random();
private static final DecimalFormat fourdf = new DecimalFormat("0000");
private static final DecimalFormat sixdf = new DecimalFormat("000000");
private static Map<String, String> map = new HashMap<>();
public static String getFourBitRandom() {
map.put("code", fourdf.format(random.nextInt(10000)));
return JSONObject.toJSONString(map);
}
public static String getSixBitRandom() {
map.put("code", sixdf.format(random.nextInt(1000000)));
return JSONObject.toJSONString(map);
}
}
阿里云以及短信服务的一些配置参数
package com.xx.utils;
/**
* @author aqi
* DateTime: 2020/11/10 11:02 上午
* Description: No Description
*/
public class ParamUtil {
public final static String KEY_ID = "";
public final static String KEY_SECRET = "";
/**
* 短信签名名称
*/
public final static String SIGN_NAME = "这个就是签名名称";
/**
* 短信模板CODE
*/
public final static String TEMPLATE_CODE = "这个就是模板CODE";
/**
* 阿里云短信API产品域名,固定写法
*/
public final static String DOMAIN = "dysmsapi.aliyuncs.com";
}
AccessKey和Secret这两个参数是在这里申请的,这两个参数相当于是整个阿里云的账号密码,相当于鉴权,切记不要泄露了