1.阿里有免申请的API
2.
3.
4.
5.code:
//发送短信的方法 @GetMapping("send/{phone}") public R sendMsm(@PathVariable String phone) { //1 从redis获取验证码,如果获取到直接返回 String code = redisTemplate.opsForValue().get(phone); if(!StringUtils.isEmpty(code)) { return R.ok(); } //2 如果redis获取 不到,进行阿里云发送 //生成随机值,传递阿里云进行发送 code = RandomUtil.getFourBitRandom(); Map<String,Object> param = new HashMap<>(); param.put("code","666666"); //调用service发送短信的方法 boolean isSend = msmService.send(param,phone); if(isSend) { //发送成功,把发送成功验证码放到redis里面 //设置有效时间 redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES); return R.ok().message("短信我是发送了!!!!!!"); } else { return R.error().message("短信发送失败"); } }
@Override public boolean send(Map<String, Object> param, String phone) { if(StringUtils.isEmpty(phone)) return false; DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI5tSFZGiUvUAqGy3QuCyn", "YZbp91zL63DO1FFSraKUxME3VgKIEg"); IAcsClient client = new DefaultAcsClient(profile); //设置相关固定的参数 CommonRequest request = new CommonRequest(); //request.setProtocol(ProtocolType.HTTPS); request.setMethod(MethodType.POST); request.setDomain("dysmsapi.aliyuncs.com"); request.setVersion("2017-05-25"); request.setAction("SendSms"); //设置发送相关的参数 request.putQueryParameter("PhoneNumbers",phone); //手机号 request.putQueryParameter("SignName","阿里云短信测试"); //申请阿里云 签名名称 request.putQueryParameter("TemplateCode","SMS_154950909"); //申请阿里云 模板code request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); //验证码数据,转换json数据传递 try { //最终发送 CommonResponse response = client.getCommonResponse(request); boolean success = response.getHttpResponse().isSuccess(); return success; }catch(Exception e) { e.printStackTrace(); return false; } }
版权声明:本文为weixin_61503139原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。