688
社区成员
发帖
与我相关
我的任务
分享| 这个作业属于哪个课程 | 2023年福大-软件工程实践-W班 |
|---|---|
| 这个作业要求在哪里 | 软件工程实践总结&个人技术博客 |
| 这个作业的目标 | 课程回顾与总结、个人技术总结 |
| 其他参考文献 | 《构建之法》 |
短信验证码是通过发送验证码到手机的一种有效的验证码系统。主要用于验证用户手机的合法性及敏感操作的身份验证。常见的使用场景有:登录注册、修改用户信息等。该技术的难点主要是将阿里短信服务接入springboot项目中
主要是登录阿里云官网,购买短信服务后,申请签名和模板即可。参考SpringBoot整合阿里云短信服务详细过程
在pom.xml中导入相关依赖
<!--阿里云短信包-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0</version>
</dependency>
编写随机生成验证码类(数字)
/**
* 随机生成验证码类
*/
public class RandomUtils {
private static final Random random = new Random();
private static final DecimalFormat fourdf = new DecimalFormat("0000");
private static final DecimalFormat sixdf = new DecimalFormat("000000");
public static String getFourBitRandom() {
return fourdf.format(random.nextInt(10000));
}
public static String getSixBitRandom() {
return sixdf.format(random.nextInt(1000000));
}
/**
* 给定数组,抽取n个数据
* @param list
* @param n
* @return
*/
public static ArrayList getRandom(List list, int n) {
Random random = new Random();
HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
// 生成随机数字并存入HashMap
for (int i = 0; i < list.size(); i++) {
int number = random.nextInt(100) + 1;
hashMap.put(number, i);
}
// 从HashMap导入数组
Object[] robjs = hashMap.values().toArray();
ArrayList r = new ArrayList();
// 遍历数组并打印数据
for (int i = 0; i < n; i++) {
r.add(list.get((int) robjs[i]));
System.out.print(list.get((int) robjs[i]) + "\t");
}
System.out.print("\n");
return r;
}
}
编写发送短信类
/**
* 发送验证码工具类
*/
public class SendCodeUtils {
/**
* 发送验证码
* @param param 验证码
* @param phone 电话号码
* @return
*/
public static boolean sendCode(Map<String, Object> param, String phone) {
if(StringUtils.isEmpty(phone)) {
return false;
}
//default 地域节点,默认就好 后面是 阿里云的 id和秘钥(这里记得去阿里云复制自己的id和秘钥哦)
DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI5tDrzTVr6b5k8edqtfaR", "BsGTQvpUWOvgZV3y4C2sqO27SYbloO");
IAcsClient client = new DefaultAcsClient(profile);
//设置相关参数
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain("dysmsapi.aliyuncs.com");
request.setSysVersion("2017-05-25");
request.setSysAction("SendSms");
// 手机号
request.putQueryParameter("PhoneNumbers", phone);
// 阿里云的签名名称
request.putQueryParameter("SignName", "closetmate");
// 阿里云的模板code
request.putQueryParameter("TemplateCode", "SMS_460660149");
// 短信模板变量对应的实际值
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
JSONObject jsonObject = JSON.parseObject(response.getData());
String code = (String) jsonObject.get("Code");
if (!code.equals("OK")) {
return false;
}
return response.getHttpResponse().isSuccess();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
使用
// 生成验证码的随机值
String code = RandomUtils.getSixBitRandom();
Map<String,Object> param = new HashMap<>();
param.put("code", code);
// 调用方法,发送短信
boolean isSend = SendCodeUtils.sendCode(param,phone.trim());
阿里短信云服务的限制是考虑到短信的合法性、可靠性以及短信内容的安全等因素,是一个解决不了的问题,需要我们在使用过程中合理规划短信发送频率。
阿里短信云服务提供了完整的API接口和图形化界面,方便开发人员使用。另外,阿里短信云服务提供了多种编程语言SDK,可以轻松接入各种开发语言中,满足各种开发需求。总之,阿里短信云服务简单易用,基本可以满足各种行业和业务的需求。