1、这里默认你已经开通了阿里云短信服务
SendSms_短信服务_API调试-阿里云OpenAPI开发者门户
2、pom.xml引入依赖
<!-- 阿里云短信依赖-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.16</version>
</dependency>
<!-- fastjson2依赖 -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.24</version>
</dependency>
<!-- redisson依赖 -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.13.6</version>
</dependency>
查看阿里云SDK
3、新建工具类,生成6个随机数,新建package名util,类名RandomUtil
package com.xxxx.util;
import java.text.DecimalFormat;
import java.util.*;
/**
* 随机数生成 工具类
*/
public class RandomUtil {
private static final DecimalFormat sixdf = new DecimalFormat("000000");
/**
* 生成6个随机数
* @return
*/
public static String getSixBitRandom() {
return sixdf.format(random.nextInt(1000000));
}
}
4.新建工具类,发送阿里云验证码,类名为:SendSmsUtil
package com.xxxx.util;
import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.ibatis.reflection.ExceptionUtil;
import java.util.Map;
/**
* 阿里云 发生 短信验证码 工具类
* mobile 手机号码
* param 验证码6位随机数
*/
public class SendSmsUtil {
public static boolean sendCode(String mobile, Map<String, String> param) {
DefaultProfile profile = DefaultProfile.getProfile("default", "您的 AccessKey ID", "必填,您的 AccessKey Secret");
IAcsClient client = new DefaultAcsClient(profile);
//设置相关固定的参数
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
//设置发送相关的参数
request.putQueryParameter("PhoneNumbers", mobile); //手机号
request.putQueryParameter("SignName","签名名称"); //申请阿里云 签名名称
request.putQueryParameter("TemplateCode","模板code"); //申请阿里云 模板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();
System.out.println(e);
System.out.println(ExceptionUtil.unwrapThrowable(e));
System.out.println(e.getMessage());
return false;
}
}
}
5、user控制层
package com.xxxx.controller;
/**
* user用户控制器
*/
@RestController
@Validated
public class UserController {
@Autowired
RedissonService redissonService;
/**
* 验证手机号 发生验证码
*/
@PostMapping("/user/send")
public ApiRestResponse sendCode(@NotBlank(message = "手机号不能为空") @Pattern(regexp = "^[1][3,4,5,6,7,8,9][0-9]{9}$", message = "手机号格式有误") String mobile) {
//生成6位随机验证码
String code = RandomUtil.getSixBitRandom();
//保存验证码到redis
Boolean isExist = redissonService.saveCodeToRedis(mobile, code);
if(!isExist) {
//阻止重复获取验证码
return ApiRestResponse.error(400,"您已获取过验证码了");
}
//转成HashMap
Map<String, String> param = new HashMap<>();
param.put("code", code);
//发送验证码
boolean res = SendSmsUtil.sendCode(mobile, param);
if(!res) {
return ApiRestResponse.success("短信验证码发送失败");
}
return ApiRestResponse.success("短信验证码发送成功");
}
}
6、redisson服务层 保存和验证code值
package com.xxxx.service.impl;
import com.xxxx.service.RedissonService;
import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class RedissonServiceImpl implements RedissonService {
/**
* 把手机号 和 验证码 保存到 redisson去
* @param mobile
* @param code
* @return
*/
@Override
public Boolean saveCodeToRedis(String mobile, String code) {
RedissonClient client = Redisson.create();
//通过手机号 检索是否已存在
RBucket<String> bucket = client.getBucket(mobile);
boolean exists = bucket.isExists();
if(!exists) {
//如果不存在 说明是第一次放进去的 往redis set设置值
bucket.set(code, 60, TimeUnit.SECONDS);
//返回true 表示存储成功 在60秒内 这个值一直存在,避免用户重复获取验证码
return true;
}
return false;
}
}
package com.xxxx.service;
public interface RedissonService {
Boolean saveCodeToRedis(String mobile, String code);
}
最后就可以使用postman等调试工具调试了。
http://localhost:8080/user/send