1.开通阿里云短信验证服务
1、开启子用户
2、新建一个用户组(设置添加权限sms)
3、创建一个用户(具体用来操作的账号)
4、得到 AccessKey(id,密码)
注意:这个账号要保存到本地,如果泄露了,要及时禁用或删除
5、找到短信控制台面板
6、添加短信模板
- 短信具体内容
- 等待审核通过(需要正当的理由)
7、添加签名
- 公司的名称
- 等待审核通过(需要正当的理由)
8、编写代码测试
- 新建一个Spring Boot 项目
- 导入依赖
<!--阿里云短信验证-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
<!--用redis设置验证码过期时间-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 测试
@SpringBootTest
class DuanxinApplicationTests {
@Test
void contextLoads() {
//连接阿里云
//这里就要填写前面说到的accessKey 的账号密码 ,确保是卡通了短信服务
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
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", "13827451008"); //手机号
request.putQueryParameter("SignName", "学习网站"); //签名名称
request.putQueryParameter("TemplateCode", "SMS_191150535"); //模板模版CODE
//验证码 ,转为json 格式
HashMap<String, Object> map = new HashMap<>();
map.put("code",6688);
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}
- 测试在手机上就会收到一条短信了
2. 真实业务开发
-
导入依赖
-
新建一个springboot 项目
-
编写配置文件application.yml, 使用阿里云服务器的redis
spring: redis: port: 6379 host: 192.168.1.1 #填写自己具体的ip
-
在service包下新建一个接口
public interface SendSms {
//参数分别为 手机号,模板,验证码
public boolean send(String phoneNum, String templateCode, Map<String,Object> map);
}
- 在serviceImp包下新建实现类
@Service
public class SendSmsImpl implements SendSms {
@Override
public boolean send(String phoneNum, String templateCode, Map<String,Object> map) {
//连接阿里云
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", <accessKeyId>", "<accessSecret>");
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", phoneNum); //手机号
request.putQueryParameter("SignName", "学习网站"); //签名
request.putQueryParameter("TemplateCode", templateCode); //模板
//验证码 ,转为json 格式
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
return response.getHttpResponse().isSuccess(); //判断是否成功
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return false;
}
}
- 在controller 包下新建一个SendSmsController类
@RestController
@CrossOrigin //跨域支持
public class SendSmsController {
@Autowired
private SendSms sendSms;
@Autowired
private RedisTemplate<String, String> redisTemplate;
@GetMapping("/send/{phone}")
public String codeSend(@PathVariable("phone") String phone){
//查询所有,是否有这个验证码,避免这个手机号频繁发送验证码
String code = redisTemplate.opsForValue().get(phone);
if(!StringUtils.isEmpty(code)){
return "exit";
}
//生成验证码并存储到redis中
//code = UUID.randomUUID().toString().substring(0, 4);//截取4位数
code = String.valueOf((int)(Math.random() * 1000000)); //生成一个6位的随机数
HashMap<String, Object> param = new HashMap<>();
param.put("code",code);
//调用发送方法
boolean isSend = sendSms.send(phone, "SMS_191150535", param);
System.out.println(isSend);
if (isSend) {
//发送成功,添加到redis缓存,并且设置5分钟有效
redisTemplate.opsForValue().set(phone,code,5,TimeUnit.MINUTES);
return "success";
}else{
return "false";
}
}
}
- 在resource的static下新建index.html ,导入jQuery 和bootstrap的包
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>短信验证</title>
<link href="./css/bootstrap.min.css" rel="stylesheet">
<script src="./js/jquery-3.4.1.min.js"></script>
<script src="./js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
<div id="msg"></div>
<form class="form-inline" style="padding: 15px;margin: 0 auto;">
<div class="form-group">
<label for="phone">手机:</label>
<input type="text" class="form-control" id="phone" name="phone" placeholder="手机号">
</div>
<input id="btn-id" class="btn btn-default" type="button" value="获取验证码">
</form>
</div>
</body>
<script>
$(function () {
//绑定发送按钮
$("#btn-id").click(function () {
let val = $("#phone").val();
$.ajax({
url: "/send/" + val,
type: "get",
success: function (result) {
console.log(result)
if (result == "exit") {
console.log("验证码已经存在");
//提示信息
let msg = '<div class="alert alert-warning" role="alert">验证码已经存在!</div>';
$('#msg').html(msg);
setTimeout(function(){
//5秒钟清空
$('#msg').empty();},5000);
} else if (result == "success"){
console.log("发送成功");
var count = 60;
var countdown = setInterval(CountDown, 1000);//间隔1秒执行一次
function CountDown() {
$("#btn-id").attr("disabled", true);
$("#btn-id").val( count + "秒后重新发送");
if (count == 0) {
$("#btn-id").val("发送验证码").removeAttr("disabled");
clearInterval(countdown);
}
count--;
}
let msg = '<div class="alert alert-warning" role="alert">发送成功!</div>';
$('#msg').html(msg);
setTimeout(function(){
$('#msg').empty();
},5000);
}else {
console.log("发送失败");
let msg = '<div class="alert alert-warning" role="alert">发送失败!</div>';
$('#msg').html(msg);
setTimeout(function(){
$('#msg').empty();
},5000);
}
}
})
});
})
</script>
</html>
-
启动项目运行测试
- 在页面输入手机号发送
- 查看手机