发表于: 2020-07-18 23:07:59
1 1207
今天完成的事:
注册阿里云,实现短信验证码发送验证
打开帮助文档,按照下面的流程完成短信服务认证
打开API Explore查看示例代码
public class SendSms {
public static void main(String[] args) {
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("RegionId", "cn-hangzhou");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}
具体的实现
写一个发送短信的接口
public interface SendSms {
public boolean send(String phoneNum, String templateCode, Map<String, Object> code);
}
写一个他的实现类
@Service
public class SendSmsImpl implements SendSms {
@Override
public boolean send(String phoneNum, String templateCode, Map<String, Object> code) {
//连接阿里云 LTAI4GCZATVrQw4yHpUaZuBP
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G7F1aoDRve8P9nGMapQ", "dsHYtvo0ZGp9e9bSH8eqWPszavj2J1");
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", "ABC商城");
request.putQueryParameter("TemplateCode", templateCode);
//request.putQueryParameter("RegionId", "cn-hangzhou");
//构建一个短信验证码
// HashMap<String,Object> map = new HashMap<>();
// map.put("code",2233);
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层,调接口
@Controller
//@CrossOrigin//跨域支持
public class SmsApiController {
@Autowired
private SendSms sendSms;
@Autowired
private RedisTemplate<String,String> redisTemplate;
@RequestMapping(value = "/send",method = RequestMethod.POST)
public String code(@RequestParam(value = "phone",required = false)String phone, Model model){
//调用发送方法 (模拟真实业务 redis)
String code = redisTemplate.opsForValue().get(phone);
//判断验证码是否过期,如果不为空 ! 直接返回
// if (!StringUtils.isEmpty(code)){
//return phone + ":" + code + "已存在,还没有过期";
// }
//生成验证码并存储到reids中
code = UUID.randomUUID().toString().substring(0, 4);
HashMap<String,Object> param = new HashMap<>();
param.put("code",code);
boolean isSend = sendSms.send(phone,"SMS_196651183",param);
if (isSend){
redisTemplate.opsForValue().set(phone,code,5000, TimeUnit.SECONDS);//5分钟过期
//return phone + ":" + code + "发送成功!";
}else {
model.addAttribute("error","请检查电话号码");
return "register";
//return "发送失败";
}
return "register";
}
}
编写注册页面,这里直接用的转发,所以会导致短信发送后,页面会刷新电话号码没了。。
<form action="${pageContext.request.contextPath}/register" method="post">
用户名:<input type="text" name="name"> <br>
密码:<input type="password" name="password"> <br>
手机:<input type="phone" name="phone">
<input type="submit" formaction="send" value="获取短信验证码"/><br>
验证码: <input type="text" name="sms"><br>
<input type="submit" value="注册">
</form>
${error}${msg}
</body>
</html>
后面再弄Ajax
评论