Java+阿⾥云⼿机验证码发送和验证
Java+阿⾥云⼿机验证码发送和验证
作者:阳旭⽹络
技术依赖:springboot技能掌握:java,springboot框架,redis
2.2要发送⼿机验证的接⼝
tAcsClient;
ient;
sRequest;
sRespon;
Exception;
tProfile;
tProfile;
.*;
red;
red;
BootApplication;
tion.*;
rvletRequest;
ssion;
;
@RestController
@SpringBootApplication
@CrossOrigin(origins="*",maxAge=3600)//解决跨域的请求
@RequestMapping(value="/api/code")
publicclassPhoneCodeController{
//短信API产品名称(短信产品名固定,⽆需修改)
staticprivatefinalStringproduct="Dysmsapi";
//短信API产品域名(接⼝地址固定,⽆需修改)
staticprivatefinalStringdomain="";
staticprivatefinalStringaccessKeyId="";
staticprivatefinalStringaccessKeySecret="";
@RequestMapping(value="/phoneCode",method=)
@ResponBody
publicstringndCode(HttpServletRequestreq)throwsClientException{
//设置超时时间-可⾃⾏调整
perty("tConnectTimeout","60000");
perty("tReadTimeout","60000");
StringurPhone=ameter()("urPhone");
//验证码code
Stringcode=domNumCode(4);
//短信签名
StringsignName="";//写你⾃⼰申请的签名名称
//短信模板
StringtemplateCode="";//短信模板的CODE
//初始化ascClient,暂时不⽀持多region(请勿修改)
IClientProfileprofile=file("cn-hangzhou",accessKeyId,accessKeySecret);
point("cn-hangzhou","cn-hangzhou",product,domain);
IAcsClientacsClient=newDefaultAcsClient(profile);
//组装请求对象
SendSmsRequestrequest=newSendSmsRequest();
neNumbers(urPhone);
nName(signName);
plateCode(templateCode);
plateParam("{"name":""+urPhone+"","code":""+code+""}");
//请求失败这⾥会抛ClientException异常
SendSmsResponndSmsRespon=Respon(request);
if(e()!=null&&e().equals("OK")){
try{
("code"+urPhone,code,600);
}catch(Exceptione){
tackTrace();
return"";
}
return"success";
}el{
returnnull;
2.3⽣成随机数字和redis的⼯具类
returnnull;
}
}
;
publicclassRandomCode{
/**
*⽣成数字验证码
*@paramnumber
*@return
*/
publicstaticStringgetRandomNumCode(intnumber){
StringcodeNum="";
int[]numbers={0,1,2,3,4,5,6,7,8,9};
Randomrandom=newRandom();
for(inti=0;i
//⽬的是产⽣⾜够随机的数,避免产⽣的数字重复率⾼的问题
intnext=t(10000);
codeNum+=numbers[next%10];
}
n(codeNum);
returncodeNum;
}
/**
*⽣成数字+字母的验证码
*@paramnumber
*@return
*/
publicstaticStringgetRandomCode(intnumber){
StringcodeNum="";
int[]code=newint[3];
Randomrandom=newRandom();
for(inti=0;i
intnum=t(10)+48;
intupperca=t(26)+65;
intlowerca=t(26)+97;
code[0]=num;
code[1]=upperca;
code[2]=lowerca;
codeNum+=(char)code[t(3)];
}
n(codeNum);
returncodeNum;
}
}
Redis的⼯具类
;
Factory;
;
;
ool;
oolConfig;
/**
*redis⼯具类
*@author阳旭⽹络
*/
publicclassRedisClientServer{
privatestaticfinalLoggerlogger=ger();
privatestaticJedisPooljedisPool=null;
privatestaticJedisjedis=null;
publicstaticJedisjedis_object=null;
privatestaticStringhost="127.0.0.1";
//privatestaticStringpassword="";
privatestaticIntegerport=6379;
static{
if(jedisPool==null){
JedisPoolConfigconfig=newJedisPoolConfig();
//设置最⼤连接数
Total(500);
//设置最⼤空闲数
Idle(20);
//设置最⼩空闲数
Idle(8);
//设置超时时间
WaitMillis(3000);
//Idle时进⾏连接扫描
tWhileIdle(true);
//表⽰idleobjectevitor两次扫描之间要sleep的毫秒数
eBetweenEvictionRunsMillis(30000);
//表⽰idleobjectevitor每次扫描的最多的对象数
TestsPerEvictionRun(10);
//表⽰⼀个对象⾄少停留在idle状态的最短时间,然后才能被idleobjectevitor
//扫描并驱逐;这⼀项只有在timeBetweenEvictionRunsMillis⼤于0时才有意义
EvictableIdleTimeMillis(60000);
//初始化连接池
jedisPool=newJedisPool(config,host,port);
jedis_object=newJedis(host,port);
}
}
privateRedisClientServer(){
}
privatestaticJedisgetJedisInstance(){
try{
if(null==jedis){
jedis=ource();
//(password);
}
}catch(Exceptione){
("实例化jedis失败.........",e);
}
returnjedis;
}
/**
*向缓存中设置字符串内容
*@author阳旭⽹络
*@author阳旭⽹络
*@date
*/
publicstaticbooleant(Stringkey,Stringvalue)throwsException{
Jedisjedis=null;
try{
jedis=getJedisInstance();
(key,value);
returntrue;
}catch(Exceptione){
("redist⽅法失败...key="+key+"value="+value,e);
}finally{
();
}
returnfal;
}
/**
*向缓存中设置字符串内容,设置过期时间
*@author阳旭⽹络
*@date
*/
publicstaticbooleant(Stringkey,Stringvalue,Integerconds)throwsException{
Jedisjedis=null;
try{
jedis=getJedisInstance();
(key,value);
(key,conds);
returntrue;
}catch(Exceptione){
("redist⽅法失败...key="+key+"value="+value,e);
}finally{
();
}
returnfal;
}
/**
*根据key获取内容
*@author阳旭⽹络
*@date
*/
publicstaticObjectget(Stringkey){
Jedisjedis=null;
try{
jedis=getJedisInstance();
Objectvalue=(key);
returnvalue;
}catch(Exceptione){
("redisget⽅法失败...key="+key);
}finally{
();
}
returnnull;
}
/**
*删除缓存中得对象,根据key
*@author阳旭⽹络
*@date
*/
publicstaticbooleandel(Stringkey){
Jedisjedis=null;
try{
jedis=getJedisInstance();
(key);
(key);
returntrue;
}catch(Exceptione){
tackTrace();
}finally{
();
}
returnfal;
}
/**
*根据key获取对象
*@author阳旭⽹络
*@date
*/
publicstatic
Jedisjedis=null;
try{
jedis=getJedisInstance();
Stringvalue=(key);
bject(value,clazz);
}catch(Exceptione){
tackTrace();
}finally{
();
}
returnnull;
}
/**
*设置key过期
*@author
*@date
*/
publicstaticbooleanexpire(Stringkey,intconds){
Jedisjedis=null;
try{
jedis=getJedisInstance();
(key,conds);
returntrue;
}catch(Exceptione){
tackTrace();
}finally{
();
}
returnfal;
}
/**
*判断是否存在key
*@author
*@date
*/
publicstaticBooleanexists(Stringkey){
Jedisjedis=null;
try{
jedis=getJedisInstance();
(key);
}catch(Exceptione){
tackTrace();
returnfal;
}finally{
();
}
}
三、发送验证码及验证
写⼀个验证的接⼝
}
@RequestMapping(value="/register",method=)
@ResponBody
publicStringregister(HttpServletRequestrequest){
try{
Stringcode=ameter("code");
if(!(("code"+urPhone+page))){
return“error”
}
returnnull;
}catch(Exceptione){
throwe;
}
}
本文发布于:2023-03-03 08:48:01,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/1677804482121168.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:手机验证码是多少.doc
本文 PDF 下载地址:手机验证码是多少.pdf
留言与评论(共有 0 条评论) |