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

来源:互联网 发布:搜索引擎自然排名算法 编辑:程序博客网 时间:2024/04/29 09:39

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

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

通过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>
  • 1
  • 2
  • 3
  • 4
  • 5

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

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;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

产生验证码部分代码:

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

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