您的位置 首页 > 数码极客

【快速安全登录】短信验证码登录过程思路及详细步骤

Part1业务流程

1.手机验证码配置:使用random对象生成所需的随机数作为验证码(例如,4位数验证码:1000~9999之间的随机数)。

2.使用接口向SMS平台发送手机号码和验证码数据,然后在SMS平台将验证码发送到开发手机号码。接口参数通常包括大象手机号码、随机验证码(或过期时间)、平台接口地址、平台密码。

3.存储接口返回的信息(通常是JSON文本数据,必须转换为JSON对象格式)。

4.手机号码-验证码、操作时间放在会话内,用作后置验证。

接收用户编写的验证码和其他数据。

6.比较提交的验证码和Session的验证码是否匹配,并判断提交作业是否在有效期内。

7.验证码正确,在有效期内请求通过,处理相关业务。

Part2首先添加jar包

工具类是指

!-超滴云jar包-

Dependency

GroupIdcommons-codec/groupId

工件commons-codec/工件id

版本1.11/版本

/dependency

Part3编写简单的短信验证功能

我只是用其他语音验证的话,用简单的短信验证功能。等等,等等,等等,等等,等等,等等,等等,等等。

创建Part4 http请求工具类

公共类http util

{

/* *

*配置公共参数timestamp、SIG和respDataType

*

* @return

*/

公共静态字符串createcommonparam

{

//时间戳

simpledate format SDF=new simpledate format(' yyymmdd hhm MSS ');

String timestamp=(新日期);

//签名

string SIG=Dige Con timestamp);

return ' timestamp=' timestamp ' SIG=' SIG ' resp data type=' con;

}

/* *

* post请求

*

* @param URL

*功能和操作

* @param body

*要开机自检的数据

* @return

* @throws IOException

*/

public static string post(string URL、string body)

{

Sy(' URL : ' Sy URL);

Sy(' body : ' Sy body);

String result=

三一

{

OutputStreamWriter out=;

buffered reader in=;

URL real URL=new URL(URL);

urlconnectionconn=real URL . open connection;

//设置连接参数

conn . setdooutput(true);

conn . setdoinput(true);

conn . setconnecttimeout(5000);

conn . setreadtimeout(20000);

conn . setrequestproperty(' content-type ',' application/x-www-form-urlencoded ');

//提交数据

Out=new output streamwriter,' utf-8 ');

out . write(body);

Out.flush

//读取返回数据

in=new buffered reader(new input stream reader,' utf-8 ');

String line=

Boolean firstLine=true//读取第一行而不换行

While ((line=in.readLine)!=)

{

If (firstLine)

{

FirstLine=false

} else

{

Result=Sy

}

Result=line

}

} catch (Exception e)

{

E.printStackTrace

}

Return result

}

/* *

*回调测试工具方法

*

* @param URL

* @param reqStr

* @return

*/

public static string posthuidiao(string URL,string body)

{

String result=

三一

{

OutputStreamWriter out=;

buffered reader in=;

URL real URL=new URL(URL);

urlconnectionconn=real URL . open connection;

//设置连接参数

conn . setdooutput(true);

conn . setdoinput(true);

conn . setconnecttimeout(5000);

conn . setreadtimeout(20000);

//提交数据

Out=new output streamwriter,' utf-8 ');

out . write(body);

Out.flush

//读取返回数据

in=new buffered reader(new input stream reader,' utf-8 ');

String line=

Boolean firstLine=true//读取第一行而不换行

While ((line=in.readLine)!=)

{

If (firstLine)

{

FirstLine=false

} else

{

Result=Sy

}

Result=line

}

} catch (Exception e)

{

E.printStackTrace

}

Return result

}

}如何生成Part5四位数字

Public static String runNumber {

string str=' abcdefghijklmnopqrstuvwxyz 0123456789 ';

string builder sb=new string builder(4);

for(int I=0;i4;I)

{

char ch=(new random . nextin);

(CH);

}

SY);

字串代码=;

《返回代码》;

}

4、执行方法执行,发送成功

Public class IndustrySMS

{

private static string operation='/industry SMS/send SMS ';

专用静态字符串帐户id=con

Private static String to=

private static String SMS content='[陶瓷技术]登录确认代码:{' runNumber.toString '},如果不是本人操作,请忽略此字符。可以打印部分

/* *

*验证码通知字符

*/

Public static void execute

{

string tmpSmsContent=;

Try{

tmp SMS content=urlencoder . encode(SMS content,' utf-8 ');

}catch(Exception e){

}

字串URL=Con operation

string body=' account sid=' account sid ' to=' to ' SMS content=' tmp SMS content

h;

//提交请求

String result=H(url,body);

sy(' result 3360 ' syresult);

}以上是短信验证码登录过程的详细步骤。

作者| classabcd

来源|博客。c

关于作者: luda

无忧经验小编鲁达,内容侵删请Email至wohenlihai#qq.com(#改为@)

热门推荐