使用阿里大于平台发送短信验证码java代码实现
2017-09-06 10:52
1056 查看
使用阿里大于平台发送短信验证码java代码实现
首先需要在阿里云平台找到阿里大于并使用公司相关信息进行平台注册。然后在应用管理-》应用列列表栏创建应用,应用名为短信的最前面中括号中内容,大于官方会给你提供一个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=创建应用时获得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.完成后调用此方法便可以发送验证码。
相关文章推荐
- java项目中使用阿里大于平台进行短信发送
- php使用阿里大于短信平台发送验证码记录
- 发送短信验证码到手机(阿里大于平台) java
- Laravel5.4中使用阿里大于(鱼)发送短信验证码
- java实现SMS短信平台发送验证码功能
- Java接入阿里大于短信平台在项目中发送短信
- Laravel5中使用阿里大于(鱼)发送短信验证码
- java 实现阿里大于 发送短信demo
- java 阿里大于短信发送功能代码
- 使用第三方平台短信接口实现发送验证码
- 利用阿里大于发送短信验证码(java)
- Laravel5中使用阿里大于(鱼)发送短信验证码
- 利用阿里大于实现发送短信(JAVA版)
- 利用阿里大于实现发送短信(JAVA版)
- Linux平台,使用JavaComm3 API及SMSLib项目实现在Web Application中发送手机短信的功能
- 使用阿里大于接口实现一个简单的短信验证(Java版)
- 使用阿里大于发送短信验证码并存储到redis中
- JAVA实现利用第三方平台发送短信验证码
- 使用阿里大于发送短信验证码
- java使用阿里大于发送短信(现在已经更名为云通讯下的短信服务)