springboot实现短信验证码的发送
我使用的是阿里云短信服务
代码前的准备
1. 申请阿里云的短信服务
2. 添加签名,这里需要等待审核通过
3. 在模板管理设置自己的短信模板
下面添加模板,选择验证码,模板内容可以直接使用输入框内的示例,点击F12,通过Search Element箭头就可以找到textarea标签的内容
4.点击自己右上角的头像,进入Access Key管理
5. 它会跳出一个选项,这里建议使用子用户AccessKey,避免直接使用最高权限的账户,就像数据库那样,不可以直接把最高权限程序猿吧.
6.首先创建一个用户组
6.接着在用户组里面添加成员
强调一下,这里一定要把编程访问勾选上,接着再为该用户授予权限,
创建后,它会为你分配一个AccessKeyID和AccessKey Secret,这里一定要把它们保存到本地,一旦关闭页面,这些东西都不会再显示.
这里授予管理短信服务的权限即可
代码实现
1.创建service层,serviceImpl层,controller层
package com.wfszmg.demo.service;
import java.util.Map;
/**
* @author 无法手执玫瑰
* 2020/07/0022 13:19
*/
public interface SmsSend {
public boolean send(String phoneNum, String templateCode, Map<String,Object> code);
}
serviceImpl层代码
package com.wfszmg.demo.service.imlp;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.wfszmg.demo.service.SmsSend;
import org.springframework.stereotype.Service;
import java.util.Map;
/**
* @author 无法手执玫瑰
* 2020/07/0022 13:21
*/
@Service
public class SmsSendImpl implements SmsSend {
@Override
public boolean send(String phoneNum, String templateCode, Map<String, Object> code) {
//连接阿里云
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "自己用户的AccessKeyId", "AccessKey Secret");
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);
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code));;
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层
package com.wfszmg.demo.controller;
import com.wfszmg.demo.service.SmsSend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* @author 无法手执玫瑰
* 2020/07/0022 13:24
*/
@Controller
@CrossOrigin //跨域支持
public class smsController {
@Autowired
private SmsSend smsSend;
@GetMapping("/send/{phone}")
@ResponseBody
public String code(@PathVariable("phone") String phone){
String code = UUID.randomUUID().toString().substring(0,5);
Map<String,Object> map = new HashMap<>();
map.put("code",code);
boolean isSend = smsSend.send(phone,"自己短信模板的模板CODE(以SMS_开头的)",map);
System.out.println(phone);
if(isSend){
return "发送成功";
}else{
return "发送失败";
}
}
}
体验吧
对了,你还要往账户里面充点钱!
最后贴一下Test的代码吧,这样更把握整体的代码
package com.wfszmg.demo.utils;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
/**
* @author 无法手执玫瑰
* 2020/07/0022 10:33
*/
@SpringBootTest
public class SmsTest {
@Test
public void contextLoads(){
//连接阿里云
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "自己用户的AccessKeyId", "AccessKey Secret");
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", "电话号码");
request.putQueryParameter("SignName", "无法手执玫瑰");
request.putQueryParameter("TemplateCode", "自己短信模板的模板CODE(以SMS_开头的)");
//构建短信验证码
Map<String, Object> map = new HashMap<>();
map.put("code",123456);
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();
}
}
}
springboot实现短信验证码的发送
原文地址:https://www.cnblogs.com/wfszmg/p/13362355.html