SpringBoot整合阿里云实现短信发送验证码
·
目录
业务场景:短信发送验证码实现注册,登录...
1. 开通阿里云短信服务

2. 引入依赖
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
</dependency>
</dependencies>
3. 实现验证码发送
3.1 随机数生成工具类
package com.atguigu.service_msm.utils;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
/**
* 获取随机数
*
* @author ksj
*
*/
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");
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;
}
}
3.2 具体实现
public Boolean send(String phone) {
if (StringUtils.hasLength(phone)) {
String code = redisTemplate.opsForValue().get(phone);
if (StringUtils.hasLength(code)) {
return true;
}
code = RandomUtil.getFourBitRandom();
Map<String, String> param = new HashMap<>();
param.put("code", code);
boolean res = sendCodeToAli(phone, param);
if(res) {
redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
return true;
}
}
return false;
}
private boolean sendCodeToAli(String phone, Map<String, String> param) {
DefaultProfile profile = DefaultProfile.
getProfile("default", "LTAI5t5udETG7H7Cs9FaJuKc", "2hCC0zjYiKj11VOabJ6uObWUeqvDeW");
IAcsClient client = new DefaultAcsClient(profile);
//设置相关固定的参数
CommonRequest request = new CommonRequest();
//request.setProtocol(ProtocolType.HTTPS);
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
//设置发送相关的参数
request.putQueryParameter("PhoneNumbers",phone); //手机号
request.putQueryParameter("SignName","阿里云短信测试"); //申请阿里云 签名名称
request.putQueryParameter("TemplateCode","SMS_154950909"); //申请阿里云 模板code
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));//验证码数据,转换json数据传递
try {
//最终发送
CommonResponse response = client.getCommonResponse(request);
boolean success = response.getHttpResponse().isSuccess();
return success;
}catch(Exception e) {
e.printStackTrace();
log.error(ExceptionUtil.getExceptionMessage(e));
return false;
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)