java项目中使用阿里大于平台进行短信发送
2017-09-06 10:02
786 查看
使用阿里大于平台发送短信验证码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=创建应用时获得
通过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; } 4000 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.完成后调用此方法便可以发送验证码。
相关文章推荐
- 使用阿里大于平台发送短信验证码java代码实现
- Java接入阿里大于短信平台在项目中发送短信
- 发送短信验证码到手机(阿里大于平台) java
- java使用阿里大于发送短信(现在已经更名为云通讯下的短信服务)
- php使用阿里大于短信平台发送验证码记录
- Java使用阿里大于发送短信通知和短信验证码
- Linux平台,使用JavaComm3 API及SMSLib项目实现在Web Application中发送手机短信的功能
- Java使用阿里大于发送短信通知和短信验证码
- java 阿里大于短信发送功能代码
- 利用阿里大于实现发送短信(JAVA版)
- 使用阿里大于接口实现一个简单的短信验证(Java版)
- Laravel5中使用阿里大于(鱼)发送短信验证码
- Laravel5中使用阿里大于(鱼)发送短信验证码
- 利用阿里大于(大鱼)平台进行发送手机验证码
- .NET Core阿里大于短信发送SDK修改以及使用
- 利用阿里大于实现发送短信(JAVA版)
- Laravel5.4中使用阿里大于(鱼)发送短信验证码
- 使用“阿里大于”和 “企信通” 短信异步发送
- 利用阿里大于(大鱼)平台进行发送手机验证码
- 使用阿里大于发送短信