博客
关于我
阿里云短信业务
阅读量:343 次
发布时间:2019-03-04

本文共 2959 字,大约阅读时间需要 9 分钟。

阿里短信验证码

一 开通业务

  • 开通业务

在这里插入图片描述

  • 添加模板审核

在这里插入图片描述

  • 添加签名审核

在这里插入图片描述

  • 添加秘钥

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

提示:请记住密码,密码出现后,不会再出现

二 代码编写

在这里插入图片描述

在这里插入图片描述

  • 依赖
com.aliyun
aliyun-java-sdk-core
4.5.3
com.alibaba
fastjson
1.2.62
  • 工具类编写
package com.shu.uilts;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 java.util.UUID;public class SendSms {       public static void main(String[] args) {           DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "******", "******");        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");        //随机验证码生成        String result = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);        System.out.println(result);        //设置发送相关的参数        request.putQueryParameter("PhoneNumbers","1771x34xxxx"); //手机号        request.putQueryParameter("SignName","小xx工作室");  //申请阿里云 签名名称        request.putQueryParameter("TemplateCode","*****"; //申请阿里云 模板Code        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(result)); //验证码 格式为JSON字符串,{"code":"1234"}        try {               CommonResponse response = client.getCommonResponse(request);            System.out.println(response.getData());        } catch (ServerException e) {               e.printStackTrace();        } catch (ClientException e) {               e.printStackTrace();        }    }}
  • Redis集成
@RestController@RequestMapping("/edumsm/msm")@CrossOriginpublic class MsmController {       @Autowired    private MsmService msmService;    @Autowired    private RedisTemplate
redisTemplate; @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
param = new HashMap<>(); param.put("code",code); //调用service发送短信的方法 boolean isSend = msmService.send(param,phone); if(isSend){ //发送成功,把发送成验证码放到Redis中 //设置有效时间 5分钟 redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES); return R.ok(); }else { return R.error().message("短信发送失败"); } }}

转载地址:http://ghbe.baihongyu.com/

你可能感兴趣的文章
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>