阿里大于平台短信验证码java代码实现

 

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

通过spring将参数注入到DefaultTaobaoClient中

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

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

 
  1.  
    public class SendVerificationCodeServiceImpl implements SendVerificationCodeService{
  2.  
    @Resource
  3.  
    private SessionFactory sessionFactory;
  4.  
    @Resource
  5.  
    private LogService logService;
  6.  
    //阿里大鱼固定url
  7.  
    private String url;
  8.  
    //应用名
  9.  
    private String appKey;
  10.  
    //应用密码
  11.  
    private String appSecret ;
  12.  
    //通过spring将参数注入后的client将行实例化
  13.  
    @Autowired
  14.  
    private TaobaoClient client = new DefaultTaobaoClient(url, appKey, appSecret);
  15.  
    private AlibabaAliqinFcSmsNumSendRequest req = commonConnect();
  16.  
     
  17.  
    /** 用户修改手机号的验证码
  18.  
    * */
  19.  
    @Override
  20.  
    public void updatePhone(String phone,Integer number){
  21.  
    String json = "{\"code\":\""+number+"\",\"product\":\"参数\"}";
  22.  
    req.setSmsParamString(json);
  23.  
    req.setRecNum(phone);
  24.  
    req.setSmsTemplateCode(MessageConstant.UPDATE_PHONE);
  25.  
    try {
  26.  
    AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
  27.  
    } catch (ApiException e) {
  28.  
    logService.saveLog(e.toString(), this.getClass(), 4);
  29.  
    e.printStackTrace();
  30.  
    }
  31.  
    }
  32.  
     
  33.  
    /**
  34.  
    * 用户修改密码成功后发送的通知
  35.  
    */
  36.  
    @Override
  37.  
    public void updateSuccessNotice(String phone,String memberName) {
  38.  
    //该json串中参数名需要与模板中参数一致。
  39.  
    String json = "{\"user\":\""+memberName+"\",\"product\":\"参数\"}";
  40.  
    req.setSmsParamString(json);
  41.  
    req.setSmsTemplateCode("SMS_70145486");//配置的模板id
  42.  
    try {
  43.  
    AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
  44.  
    } catch (ApiException e) {
  45.  
    logService.saveLog(e.toString(), this.getClass(), 4);
  46.  
    e.printStackTrace();
  47.  
    }
  48.  
    }
  49.  
     
  50.  
    /**
  51.  
    * 提取公共需要参数
  52.  
    * @return
  53.  
    */
  54.  
    public AlibabaAliqinFcSmsNumSendRequest commonConnect(){
  55.  
    //以下为大于官方demo中发送文本短信固定写法。
  56.  
    AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
  57.  
    req.setExtend(MessageConstant.COMMON_EXTEND);
  58.  
    req.setSmsType(MessageConstant.SMS_TYPE);
  59.  
    req.setSmsFreeSignName(MessageConstant.SMS_FREE_SIGNNAME);
  60.  
    return req;
  61.  
    }
  62.  
    public String getUrl() {
  63.  
    return url;
  64.  
    }
  65.  
    public void setUrl(String url) {
  66.  
    this.url = url;
  67.  
    }
  68.  
    public String getAppKey() {
  69.  
    return appKey;
  70.  
    }
  71.  
    public void setAppKey(String appKey) {
  72.  
    this.appKey = appKey;
  73.  
    }
  74.  
    public String getAppSecret() {
  75.  
    return appSecret;
  76.  
    }
  77.  
    public void setAppSecret(String appSecret) {
  78.  
    this.appSecret = appSecret;
  79.  
    }
  80.  
     
  81.  
     
  82.  
    }
 
一键复制

产生验证码部分代码:

 Integer code  =    (int)((Math.random()*9+1)*100000);
 
一键复制

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值