java项目中使用阿里大于平台进行短信发送

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/github_38924695/article/details/77862704

使用阿里大于平台发送短信验证码java代码实现

  1. 首先需要在阿里云平台找到阿里大于并使用公司相关信息进行平台注册。然后在应用管理-》应用列列表栏创建应用,应用名为短信的最前面中括号中内容,大于官方会给你提供一个appKey和AppSecret代码中会用到。
    这里写图片描述
    2.创建好应用后就需要在配置管理中添加对应的模板,企业用户默认会有一些模板,可以使用官方的,也可以自己创建。创建好之后需要记住模板的模板ID,以及传递参数的参数名。
    这里写图片描述
    这里写图片描述
    3.在平台上配置好之后可以在应用管理-》应用测试下进行测试,这里可以看见官方提供的demo:
    这里写图片描述
    4.测试通过后就可以进行代码开发。开发前需要下载两个jar包:
    这里写图片描述
    首先配置一个message.properties文件将url,appkey及appsecret配置好。其中url为大于官方固定的值http://gw.api.taobao.com/router/rest,另外两个参数为创建应用时的两个参数。
#短信通知平台的用户名和密码
#阿里大鱼固定url
dayu.url=http://gw.api.taobao.com/router/rest
#应用名(阿里大鱼后台创建的应用)
dayu.appKey=创建应用时获得
#应用密码
dayu.appSecret=创建应用时获得

通过spring将参数注入到DefaultTaobaoClient中

<bean id="testdayu" class="com.taobao.api.DefaultTaobaoClient">
        <constructor-arg index="0" value="${dayu.url}"/>
        <constructor-arg index="1" value="${dayu.appKey}"/>
        <constructor-arg index="2" value="${dayu.appSecret}"/>
    </bean>

在具体的实现类中将参数获取到并进行发送验证码:

public class SendVerificationCodeServiceImpl implements SendVerificationCodeService{
    @Resource
    private SessionFactory sessionFactory;
    @Resource
    private LogService logService;
    //阿里大鱼固定url
    private String  url;
    //应用名
    private String appKey;
    //应用密码
    private String appSecret ;
    //通过spring将参数注入后的client将行实例化
     @Autowired
    private TaobaoClient client = new DefaultTaobaoClient(url, appKey, appSecret);
    private AlibabaAliqinFcSmsNumSendRequest req = commonConnect();

    /** 用户修改手机号的验证码
     *  */
    @Override
    public void updatePhone(String phone,Integer number){
        String json = "{\"code\":\""+number+"\",\"product\":\"参数\"}";
        req.setSmsParamString(json);
        req.setRecNum(phone);
        req.setSmsTemplateCode(MessageConstant.UPDATE_PHONE);
        try {
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
        } catch (ApiException e) {
            logService.saveLog(e.toString(), this.getClass(), 4);
            e.printStackTrace();
        }
    }

    /**
     * 用户修改密码成功后发送的通知
     */
    @Override
    public void updateSuccessNotice(String phone,String memberName) {
    //该json串中参数名需要与模板中参数一致。
        String json = "{\"user\":\""+memberName+"\",\"product\":\"参数\"}";
        req.setSmsParamString(json);
        req.setSmsTemplateCode("SMS_70145486");//配置的模板id
        try {
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
        } catch (ApiException e) {
            logService.saveLog(e.toString(), this.getClass(), 4);
            e.printStackTrace();
        }
    }

    /**
     * 提取公共需要参数
     * @return
     */
    public AlibabaAliqinFcSmsNumSendRequest commonConnect(){
    //以下为大于官方demo中发送文本短信固定写法。
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend(MessageConstant.COMMON_EXTEND);
        req.setSmsType(MessageConstant.SMS_TYPE);
        req.setSmsFreeSignName(MessageConstant.SMS_FREE_SIGNNAME);
        return req;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getAppKey() {
        return appKey;
    }
    public void setAppKey(String appKey) {
        this.appKey = appKey;
    }
    public String getAppSecret() {
        return appSecret;
    }
    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }


}

产生验证码部分代码:

 Integer code  =    (int)((Math.random()*9+1)*100000);

这样产生的6位验证码没有以0开头的,验证码大小在111111-999999之间,防止以0开头出现验证码位数不够的问题。
5.完成后调用此方法便可以发送验证码。

猜你喜欢

转载自blog.csdn.net/github_38924695/article/details/77862704