目录
1.登录阿里云
2.找到短信服务模版
我记得这里如果你是一个新的用户的话 你是可以获得免费的100条短信的额度来使用的
3.申请资质
4.申请签名
5.申请模版
开发者文档地址 如何在短信服务控制台删除对应的短信模板_短信服务(SMS)-阿里云帮助中心 (aliyun.com)
6.获取AccessKey
创建一个AccessKey 并且记录他们的值
7.通过api编写java代码发送验证码
引入依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.24</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.0</version>
</dependency>
代码:
package com.hyh.service.impl;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.jetbrains.annotations.NotNull;
public class SmsServiceImpl {
public static String send(String phone, String key){
//第二个参数为自己独有的accessKeyid,第三个参数为自己独有的accessKeySecret
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
"accessKeyid","accessKeySecret");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = getCommonRequest(phone, key);
try {
CommonResponse response = null;
try {
response = client.getCommonResponse(request);
return response.getData();
} catch (ClientException e) {
throw new RuntimeException(e);
}
} catch (Exception e) {
e.printStackTrace();
}
return phone;
}
@NotNull
private static CommonRequest getCommonRequest(String phone, String key) {
CommonRequest request = new CommonRequest();//组装请求对象
//request.setProtocol(ProtocolType.HTTPS);
request.setMethod(MethodType.POST);//设置post提交
request.setDomain("dysmsapi.aliyuncs.com");//短信API产品域名(接口地址固定,无需修改)
request.setVersion("2017-05-25");
request.setAction("SendSms");
request.putQueryParameter("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumbers", phone);
request.putQueryParameter("SignName", "自己设置的短信签名的名字");//短信签名
request.putQueryParameter("TemplateCode", "自己短信模版的编号");
request.putQueryParameter("TemplateParam", "{code:"+ key +"}");
return request;
}
}
因为我的代码放在一个service里面所以调用就很简单 直接通过类名调用
/*
* @description: 发送短信验证码
* @param:
* @return:
* @author
* @date: 2024/1/13 20:42
*/
@PostMapping ("/sendNumber")
public Map<String,Object> login(@RequestBody User user, HttpSession session){
String key = mailService.RandomCode();//调用方法生成随机的6位数的验证码
session.setAttribute("number",user.getPhone());
session.setAttribute("key",key);
session.setMaxInactiveInterval(900);// 设置15分钟过期
String send = SmsServiceImpl.send(user.getPhone(), key);调用发短信的验证码
System.out.println(send);
Map<String,Object> map = new HashMap<>();
map.put("msg","发送成功");
map.put("state",true);
map.put("code",200);
return map;
}
// 随机生成的6位的随机数 自己可以定义一个工具类
public String RandomCode() {
Random random = new Random();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 1; i <= 6; i++) {
int nextInt = random.nextInt(10);
stringBuilder.append(nextInt);
}
return stringBuilder.toString();
}
我们可以通过前端来调用 这里用PostMan来调试一下
8.利用验证码来实现登录功能
代码
/*
* @description: 验证码的登录
* @param:
* @return:
* @author
* @date: 2024/1/13 20:43
*/
@PostMapping("/loginNumber")
public Result loginNumber(@RequestBody User user,HttpSession session,HttpServletResponse response){
String key = (String) session.getAttribute("key");//从session里面获取保存的key 也就是号码
String number = (String) session.getAttribute("number");//获取session的验证码
if(!key.equals(user.getCheCode())){
return new Result(Code.Err,null,"验证码错误");
}
User user1 = new User();
//这里懒得搞 就是应用户名和密码设置成号码 自己可以重新设置
user1.setUsername(number);
user1.setPassword(number);
user1.setEmail(number);
boolean flag = userService.save(user1);
return new Result(flag?Code.Ok:Code.SAVE_ERR,flag,"登录成功");
}
利用postman进行测试
可以看出这个功能是可以正常实现的。
注意:这个方法只能是用于测试的手机号码在阿里云上绑定过的才可以
如果需要不同手机号都能发验证码 需要申请签名的时候选择其他