zoukankan      html  css  js  c++  java
  • 阿里云短信服务

    一、在阿里云开通短信服务

    1.在阿里云系统搜索短信服务,然后点击免费开通:

     开通之后的界面如下图所示:

     2.进入控制台,点击“国内消息”,需要申请签名管理和模板管理:

     

    3.申请模板管理方式:点击添加模板申请模板管理

     填写相关信息:

     点击提交,等到审核通过即可使用。

    4.申请签名管理方式:点击添加签名申请签名管理(签名名称应该取有实际意义的名字,否则申请不通过)

     

     

     5.短信验证服务的使用

    (1)引入依赖

     <dependencies>
            <!--json转换工具-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
            </dependency>
            <!--阿里云操作核心库-->
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-core</artifactId>
            </dependency>
        </dependencies>

    (2)编写controller层

    package com.atguigu.msmservice.controller;
    
    import com.atguigu.commonutils.R;
    import com.atguigu.msmservice.service.MsmService;
    import com.atguigu.msmservice.utils.RandomUtil;
    import io.lettuce.core.dynamic.domain.Timeout;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.util.StringUtils;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    
    /**
     * author LiQinZhen
     * date 2020/10/28
     * description: 短信服务
     */
    @RestController
    @CrossOrigin
    @RequestMapping("/edumsm/msm")
    public class MsmController {
    
        @Autowired
        RedisTemplate<String,String> redisTemplate;
    
        @Autowired
        MsmService msmService;
    
        //发送短信的方法
        @GetMapping("/send/{phone}")
        public R sendMsm(@PathVariable String phone){
            //从redis中获取验证码,如果获取到,则直接返回
            String code = redisTemplate.opsForValue().get(phone);
            if (!StringUtils.isEmpty(code)){
                return R.ok();
            }
            //如果redis中获取不到,进行阿里云发送
            //调用生成随机数的工具类生成随机值,传递给阿里云进行发送
            code = RandomUtil.getFourBitRandom();
            Map<String, Object> param = new HashMap<>();
            param.put("code",code);
            Boolean isSend = msmService.send(param,phone);
            //判断是否发送成功
            if(isSend){
                //发送成功,把发送成功的验证码放入redis中
                //设置有效时间
                redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
                return  R.ok();
            }else{
                return R.error().message("短信发送失败!");
            }
        }
    
    }

    (3)编写service层

    package com.atguigu.msmservice.service;
    
    import java.util.Map;
    
    /**
     * author LiQinZhen
     * date 2020/10/28
     * description: TODO
     */
    public interface MsmService {
        //发送短信的方法
        Boolean send(Map<String, Object> param, String phone);
    }

    (4)编写service的实现类

    package com.atguigu.msmservice.service.impl;
    
    import com.alibaba.fastjson.JSON;
    import com.aliyuncs.CommonRequest;
    import com.aliyuncs.CommonResponse;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.http.HttpResponse;
    import com.aliyuncs.http.MethodType;
    import com.aliyuncs.profile.DefaultProfile;
    import com.atguigu.msmservice.service.MsmService;
    import org.springframework.stereotype.Service;
    import org.springframework.util.StringUtils;
    
    import java.util.Map;
    
    /**
     * author LiQinZhen
     * date 2020/10/28
     * description: TODO
     */
    
    @Service
    public class MsmServiceImpl implements MsmService {
        //发送短信的方法
        @Override
        public Boolean send(Map<String, Object> param, String phone) {
            //判断手机号是否为空,为空则不发送短信
            if(StringUtils.isEmpty(phone)){
                return false;
            }
            /**
             *  阿里云秘钥:
             * AccessKey ID:
             * LTAI4G6jrDAKuojWxsDcthi8
             * AccessKey Secret:
             * oi2ySN9oSRHZMVW5ni4ITcFFdAyiik
             */
            //设置阿里云秘钥
            DefaultProfile profile =
            DefaultProfile.getProfile("default", "LTAI4G6jrDAKuojWxsDcthi8", "oi2ySN9oSRHZMVW5ni4ITcFFdAyiik");
            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_205123509");//阿里云中申请的模板code
            request.putQueryParameter("TemplateParam", JSON.toJSONString(param));//验证码数据,转换为JSON数据传递
    
            try{
                //最终发送
                CommonResponse response = client.getCommonResponse(request);
                boolean success = response.getHttpResponse().isSuccess();
                return success;
    
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
    }

    (5)swagger测试

     阿里云短信服务平台也可以查到记录:

  • 相关阅读:
    bzoj2728
    bzoj4574
    loj2554
    bzoj1068
    bzoj2554
    Exception in thread "main" java.lang.AbstractMethodError
    java方法重载,java方法练习题
    java面向对象
    java编辑器 IntelliJ IDEA 安装——放弃过程;eclipse,Notepad++
    java二维数组
  • 原文地址:https://www.cnblogs.com/liqinzhen/p/13889983.html
Copyright © 2011-2022 走看看