美文网首页springboot
阿里云短信接入--springboot

阿里云短信接入--springboot

作者: 一个忙来无聊的人 | 来源:发表于2019-05-14 10:08 被阅读0次

    本篇文章分为两个模块阿里云注册和Java开发

    一:阿里云注册(没有账号?支付宝什么登录就可以啦)

    阿里短信网址 https://www.aliyun.com/product/sms

    登录后 进入下图管理控制台

    image

    按步骤 添加签名 审核通过之后就可以了 -- PS 签名就是短信最开始位置的信息 --

    添加模板 -- 一般来说验证码什么的就是一个参数啦,如果是企业群发什么的需要添加多个参数的自己配置 如下模板样例

    您的验证码${code},该验证码5分钟内有效,请勿泄漏于他人! ** 重点 code这个字段名称需要记住**

    签名名称 和 模板code 后面代码里面会用到

    image image

    现在开始生成accesskeeys 了 如下图 页面右上角,鼠标移动过去然后生成就可以了。

    image

    二:Java开发模块

    JAVA 短信接入的代码如下

    注意事项

    1、 accessKeyId 和 accessKeySecret 需要在账号信息哪儿查询更改

    2、 signName 需要更改为你的在页面上的签名信息

    3、方法请求参数 templateCode 就是在页面上维护的模板编码

    4、templateParam 模板内需要填充的字段及字段值 格式为("{"name":"Tom", "code":"123"}")

    需要引入pom 文件

    
    <dependency>
    
    <groupId>com.aliyun</groupId>
    
    <artifactId>aliyun-java-sdk-core</artifactId>
    
    <version>4.1.0</version>
    
    </dependency>
    
    

    JAVA 代码

    package com.scb.xxx.service.impl;
    
    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 com.scb.common.util.JacksonUtil;
    
    import com.scb.member.web.SmsController;
    
    import org.apache.logging.log4j.LogManager;
    
    import org.springframework.stereotype.Service;
    
    @Service
    
    public class MemberSmsServiceImpl {
    
    //替换成你的AccessKey 
    
        final String accessKeyId ="XXXX";//你的accessKeyId,
    
        final String accessKeySecret ="xxxxxx";//你的accessKeySecret
    
    /**
    
    * 短信发送接口信息  支持批量发送 ps--目前签名信息仅设置一个
    
    *
    
        * @param phone        需要发送的电话号码,支持多个电话号码 格式为"13600000000,15000000000"
    
        * @param templateCode  明确需要使用哪个模板,可以从阿里云控制台查看
    
        * @param templateParam 模板内需要填充的字段及字段值 格式为("{\"name\":\"Tom\", \"code\":\"123\"}")
    
        * @Return true 代表发送成功  false 代表发送失败
    
    */
    
        public boolean sendMsg(String phone, String templateCode, String templateParam) {
    
            boolean bool =false;
    
            DefaultProfile profile = DefaultProfile.getProfile("default",accessKeyId,accessKeySecret);
    
            IAcsClient client =new DefaultAcsClient(profile);
    
            CommonRequest request =new CommonRequest();
    
            request.setMethod(MethodType.POST);
    
            request.setDomain("dysmsapi.aliyuncs.com");
    
            request.setVersion("2017-05-25");// 版本信息  已经固定  不能进行更改
    
    //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时,接收号码格式为国际区号+号码,如“85200000000”
    
            request.putQueryParameter("PhoneNumbers", phone);
    
            //  阿里云控制台签名
    
            String signName ="填写你的签名";
    
            request.putQueryParameter("SignName", signName);
    
            // 阿里云控制台模板编号
    
            request.putQueryParameter("TemplateCode", templateCode);
    
            request.setAction("SendSms");//系统规定参数
    
            // 模板内需要填充参数信息
    
            request.putQueryParameter("TemplateParam", templateParam);
    
            try {
    
                    logger.info("调用阿里云短信服务请求 phone={},templateCode={},templateParam={}", phone, templateCode, templateParam);
    
                    CommonResponse response = client.getCommonResponse(request);
    
                    // 下面是一个json格式转换工具,把String 转换为map 也可以转换为对象
    
                    Map map = JacksonUtil.json2Map(response.getData());
    
                    if ("OK".equals(map.get("Code"))) {
    
                        bool =true;
    
                    }
    
            }catch (ServerException e) {
    
                logger.error("阿里云短信服务异常:{}", e);
    
            }catch (ClientException e) {
    
                    logger.error("连接阿里云短信异常:{}", e);
    
            }catch (Exception e) {
    
                logger.error("json转换异常:{}", e);
    
            }
    
            return bool;
    
        }
    
    }
    

    成功的返回是 response.getData()

    {"Message":"OK","RequestId":"81BC6A5A-2123-4B93-A87B-675F7C0EB137","BizId":"406900757297716634^0","Code":"OK"}

    相关文章

      网友评论

        本文标题:阿里云短信接入--springboot

        本文链接:https://www.haomeiwen.com/subject/lcvjaqtx.html