标签:template lock out try 手机 一个用户 失败 else 用户组
我使用的是阿里云短信服务
下面添加模板,选择验证码,模板内容可以直接使用输入框内的示例,点击F12,通过Search Element箭头就可以找到textarea标签的内容
示例:您的验证码为:${code},该验证码 5 分钟内有效,请勿泄漏于他人。
强调一下,这里一定要把编程访问勾选上,接着再为该用户授予权限,
创建后,它会为你分配一个AccessKeyID和AccessKey Secret,这里一定要把它们保存到本地,一旦关闭页面,这些东西都不会再显示.
这里授予管理短信服务的权限即可
service层代码
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 "发送失败";
}
}
}
对了,你还要往账户里面充点钱!
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();
}
}
}
标签:template lock out try 手机 一个用户 失败 else 用户组
原文地址:https://www.cnblogs.com/wfszmg/p/13362355.html