云之讯手机号短信验证
生活随笔
收集整理的這篇文章主要介紹了
云之讯手机号短信验证
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄標題
- 1、下載相關服務sdk
- 2、解壓
- 3、創(chuàng)建項目
- 4、導入依賴
- 5、修改配置
- 6、獲取發(fā)送短信的工具包
- 7、使用
- 8、官網(wǎng)提供的例子
1、下載相關服務sdk
地址:http://docs.ucpaas.com/doku.php?id=sms_sdk
2、解壓
加壓下載好的sdk(restdemo_java_v2.zip)
3、創(chuàng)建項目
4、導入依賴
<!--短信服務依賴--> <dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.4</version></dependency>5、修改配置
config.properties 這個文件從解壓包中獲取
6、獲取發(fā)送短信的工具包
7、使用
package com.lihua.phonecode.service;import org.springframework.stereotype.Service;/*** 獲取四位數(shù)驗證碼* @author 15594*/public interface PhoneCodeService {/**** 返回一個四位的隨機數(shù),充當手機號驗證碼* @return 字符串類型的驗證碼* */String getRandomCode();/*** 將驗證碼發(fā)送到指定手機* @return 是否發(fā)送驗證碼成功* @param code 驗證碼* @param phone 手機號* */boolean sendCode(String code,String phone);/*** 驗證碼是否過期,通過redis zset數(shù)據(jù)類型判斷* @return 返回是否過期,過期為true* @param phone 用戶手機號,通過手機號標識過期時間* */boolean isOutTime(String phone);} package com.lihua.phonecode.service.impl;import com.lihua.phonecode.service.PhoneCodeService; import com.lihua.phonecode.utils.ucpaas.restDemo.client.JsonReqClient; import lombok.Data; import org.junit.Test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service;import java.util.Random; import java.util.UUID;/*** 生產(chǎn)手機驗證碼,并發(fā)送給手機驗證。注意:使用properties的方式注入,一定要有set方法* @author 15594*/ @Service @PropertySource(value = {"classpath:config.properties"}) @ConfigurationProperties(prefix = "yzx") @Data public class PhoneCodeServiceImpl implements PhoneCodeService {/*** 短信接口需要的參數(shù),這些參數(shù)的值從config.properties文件中注入。注意:注入時必須提供set方法。這個通過lombok的@Data注解生成。*/private String sid;private String token;private String appid;private String regTemplateid;private String faultTemplateid;@Overridepublic String getRandomCode() {Random random = new Random();int code = 1000+random.nextInt(8999);return Integer.toString(code);}@Overridepublic boolean sendCode(String code, String phone) {System.out.println(sid);JsonReqClient jsonReqClient = new JsonReqClient();String result = jsonReqClient.sendSms(sid, token, appid, regTemplateid, code, phone, null);System.out.println("響應:"+result);if (result.equals("000000")){return true;}else {return false;}}@Overridepublic boolean isOutTime(String phone) {return true;}}調(diào)用sendCode方法即可將驗證碼發(fā)送到用戶手機。
返回參數(shù):
更多看官網(wǎng):http://docs.ucpaas.com/doku.php?id=error_code
8、官網(wǎng)提供的例子
官網(wǎng)提供的例子其實就在下載的zip壓縮包里
總結(jié)
以上是生活随笔為你收集整理的云之讯手机号短信验证的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 作曲大师_作曲的力量
- 下一篇: 移动硬盘格式化后还能恢复数据吗 格式化的