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