博客
关于我
阿里云短信业务
阅读量: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命令行登陆,远程登陆MySQL
查看>>
mysql命令:set sql_log_bin=on/off
查看>>
mySQL和Hive的区别
查看>>
MySQL和Java数据类型对应
查看>>
mysql和oorcale日期区间查询【含左右区间问题】
查看>>
MYSQL和ORACLE的一些操作区别
查看>>
mysql和redis之间互相备份
查看>>
MySQL和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>
Mysql在Linux运行时新增配置文件提示:World-wrirable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored 权限过高导致
查看>>
Mysql在Windows上离线安装与配置
查看>>
MySQL在渗透测试中的应用
查看>>
Mysql在离线安装时启动失败:mysql服务无法启动,服务没有报告任何错误
查看>>
Mysql在离线安装时提示:error: Found option without preceding group in config file
查看>>
MySQL基于SSL的主从复制
查看>>
Mysql基本操作
查看>>
mysql基本操作
查看>>
mysql基本知识点梳理和查询优化
查看>>
mysql基础
查看>>
Mysql基础 —— 数据基础操作
查看>>