欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

关于springboot集成阿里云短信的问题

程序员文章站 2024-01-03 10:12:34
目录1.获取签名与模板2.编写模板与签名的枚举类3.配置类4.测试类​1.获取签名与模板进入阿里云平台,进入短信服务模块,在以下位置添加签名和模板(格式一定按照要求填写 审批的比较严格)2.编写模板与...

关于springboot集成阿里云短信的问题

1.获取签名与模板

进入阿里云平台,进入短信服务模块,在以下位置添加签名和模板(格式一定按照要求填写 审批的比较严格)

关于springboot集成阿里云短信的问题

2.编写模板与签名的枚举类

在上文获取模板与签名成功后,并作为常量放在枚举类中。

public enum dysmsenum {

    /**
     * 本类此处需要修改(短信模板编码,签名,参数)
     */
    login_template_code("sms_187570276", "自定义软件框架集成百度云", "code");

    /**
     * 短信模板编码
     */
    private string templatecode;
    /**
     * 签名
     */
    private string signname;
    /**
     * 短信模板必需的数据名称,多个key以逗号分隔,此处配置作为校验
     */
    private string keys;

    private dysmsenum(string templatecode, string signname, string keys) {
        this.templatecode = templatecode;
        this.signname = signname;
        this.keys = keys;
    }

    public string gettemplatecode() {
        return templatecode;
    }

    public void settemplatecode(string templatecode) {
        this.templatecode = templatecode;
    }

    public string getsignname() {
        return signname;
    }

    public void setsignname(string signname) {
        this.signname = signname;
    }

    public string getkeys() {
        return keys;
    }

    public void setkeys(string keys) {
        this.keys = keys;
    }

    public static dysmsenum toenum(string templatecode) {
        if (stringutils.isempty(templatecode)) {
            return null;
        }
        for (dysmsenum item : dysmsenum.values()) {
            if (item.gettemplatecode().equals(templatecode)) {
                return item;
            }
        }
        return null;
    }
}

3.配置类

这里面需要两个参数accesskeyid,accesskeysecret,需要在以下位置获取。

关于springboot集成阿里云短信的问题
关于springboot集成阿里云短信的问题

import com.alibaba.fastjson.jsonobject;
import com.aliyuncs.defaultacsclient;
import com.aliyuncs.iacsclient;
import com.aliyuncs.dysmsapi.model.v20170525.sendsmsrequest;
import com.aliyuncs.dysmsapi.model.v20170525.sendsmsresponse;
import com.aliyuncs.exceptions.clientexception;
import com.aliyuncs.profile.defaultprofile;
import com.aliyuncs.profile.iclientprofile;
import com.vanpeng.systemportal.modules.dysms.constant.dysmsenum;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.stereotype.component;

@component
public class dysmshelper {

    private final static logger logger = loggerfactory.getlogger(dysmshelper.class);

    //产品名称:云通信短信api产品,无需替换
    static final string product = "dysmsapi";
    //产品域名,无需替换
    static final string domain = "dysmsapi.aliyuncs.com";

    //此处需要替换成开发者自己的ak(在阿里云访问控制台寻找 本类需要修改此处)
    static  string accesskeyid;
    static  string accesskeysecret;

    public static void setaccesskeyid(string accesskeyid) {
        dysmshelper.accesskeyid = accesskeyid;
    }

    public static void setaccesskeysecret(string accesskeysecret) {
        dysmshelper.accesskeysecret = accesskeysecret;
    }

    public static string getaccesskeyid() {
        return accesskeyid;
    }

    public static string getaccesskeysecret() {
        return accesskeysecret;
    }

    public boolean sendsms(string phone, jsonobject templateparamjson, dysmsenum dysmsenum) throws clientexception {
        //可自助调整超时时间
        system.setproperty("sun.net.client.defaultconnecttimeout", "10000");
        system.setproperty("sun.net.client.defaultreadtimeout", "10000");
        //初始化acsclient,暂不支持region化
        iclientprofile profile = defaultprofile.getprofile("cn-hangzhou", accesskeyid, accesskeysecret);
        defaultprofile.addendpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        iacsclient acsclient = new defaultacsclient(profile);
        //验证json参数
        validateparam(templateparamjson, dysmsenum);
        //组装请求对象-具体描述见控制台-文档部分内容
        sendsmsrequest request = new sendsmsrequest();
        //必填:待发送手机号
        request.setphonenumbers(phone);
        //必填:短信签名-可在短信控制台中找到
        request.setsignname(dysmsenum.getsignname());
        //必填:短信模板-可在短信控制台中找到
        request.settemplatecode(dysmsenum.gettemplatecode());
        //可选:模板中的变量替换json串,如模板内容为"亲爱的${name},您的验证码为$[code]"时,此处的值为
        request.settemplateparam(templateparamjson.tojsonstring());
        //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
        //request.setsmsupextendcode("90997");
        //可选:outid为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        //request.setoutid("youroutid");
        boolean result = false;
        //hint 此处可能会抛出异常,注意catch
        sendsmsresponse sendsmsresponse = acsclient.getacsresponse(request);
        logger.info("短信接口返回的数据----------------");
        logger.info("{code:" + sendsmsresponse.getcode() + ",message:" + sendsmsresponse.getmessage() + ",requestid:" + sendsmsresponse.getrequestid() + ",bizid:" + sendsmsresponse.getbizid() + "}");
        if ("ok".equals(sendsmsresponse.getcode())) {
            result = true;
        }
        return result;
    }

    private static void validateparam(jsonobject templateparamjson, dysmsenum dysmsenum) {
        string keys = dysmsenum.getkeys();
        string[] keyarr = keys.split(",");
        for (string item : keyarr) {
            if (!templateparamjson.containskey(item)) {
                throw new runtimeexception("模板缺少参数:" + item);
            }
        }
    }
}

4.测试类

使用postman或者其他方式调用即可发送短信。

@restcontroller
@requestmapping("/sendsmscontrollor")
public class sendsmscontrollor {

    @autowired
    dysmshelper dysmshelper;

    @requestmapping("/sendsms")
    public void sendsms() throws clientexception {
        jsonobject obj = new jsonobject();
        obj.put("code", "1234");
        dysmshelper.sendsms("电话号", obj, dysmsenum.login_template_code);
    }
}

注意:一定要往账户中充钱或者办理套餐,否则短信发不出去的。

到此这篇关于springboot集成阿里云短信的文章就介绍到这了,更多相关springboot阿里云短信内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

上一篇:

下一篇: