Java短信验证码登录流程思路及详细步骤
Java专栏
共 3853字,需浏览 8分钟
·
2021-10-09 09:57
点击关注公众号,Java干货及时送达
牛逼!又发现了一款面试题库,太全了!!
(点击查看)
Part2首先添加一个jar包
工具类会用到
<dependency>
<groupId>commons-codecgroupId>
<artifactId>commons-codecartifactId>
<version>1.11version>
dependency>
Part3编写简单的短信验证功能
我这里只是编写一个简单的短信验证功能,要是用其他的语音验证。。。。等等需要去秒滴云官方下载文档,下面是编写的一个config文档,专门存放一些参数
Part4编写http请求工具类
public class HttpUtil
{
/**
* 构造通用参数timestamp、sig和respDataType
*
* @return
*/
public static String createCommonParam()
{
// 时间戳
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timestamp = sdf.format(new Date());
// 签名
String sig = DigestUtils.md5Hex(Config.ACCOUNT_SID + Config.AUTH_TOKEN + timestamp);
return "×tamp=" + timestamp + "&sig=" + sig + "&respDataType=" + Config.RESP_DATA_TYPE;
}
/**
* post请求
*
* @param url
* 功能和操作
* @param body
* 要post的数据
* @return
* @throws IOException
*/
public static String post(String url, String body)
{
System.out.println("url:" + System.lineSeparator() + url);
System.out.println("body:" + System.lineSeparator() + body);
String result = "";
try
{
OutputStreamWriter out = null;
BufferedReader in = null;
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
// 设置连接参数
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(5000);
conn.setReadTimeout(20000);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 提交数据
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(body);
out.flush();
// 读取返回数据
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = "";
boolean firstLine = true; // 读第一行不加换行符
while ((line = in.readLine()) != null)
{
if (firstLine)
{
firstLine = false;
} else
{
result += System.lineSeparator();
}
result += line;
}
} catch (Exception e)
{
e.printStackTrace();
}
return result;
}
/**
* 回调测试工具方法
*
* @param url
* @param reqStr
* @return
*/
public static String postHuiDiao(String url, String body)
{
String result = "";
try
{
OutputStreamWriter out = null;
BufferedReader in = null;
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
// 设置连接参数
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(5000);
conn.setReadTimeout(20000);
// 提交数据
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(body);
out.flush();
// 读取返回数据
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = "";
boolean firstLine = true; // 读第一行不加换行符
while ((line = in.readLine()) != null)
{
if (firstLine)
{
firstLine = false;
} else
{
result += System.lineSeparator();
}
result += line;
}
} catch (Exception e)
{
e.printStackTrace();
}
return result;
}
}
Part5生成四位数的方法
public static String runNumber() {
String str="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuilder sb=new StringBuilder(4);
for(int i=0;i<4;i++)
{
char ch=str.charAt(new Random().nextInt(str.length()));
sb.append(ch);
}
System.out.println(sb.toString());
String code = sb.toString();
return code;
}
4、执行方法execute(),便会发送成功
public class IndustrySMS
{
private static String operation = "/industrySMS/sendSMS";
private static String accountSid = Config.ACCOUNT_SID;
private static String to = "15342349382";
private static String smsContent = "【小陶科技】登录验证码:{"+runNumber().toString()+"},如非本人操作,请忽略此短信。";
/**
* 验证码通知短信
*/
public static void execute()
{
String tmpSmsContent = null;
try{
tmpSmsContent = URLEncoder.encode(smsContent, "UTF-8");
}catch(Exception e){
}
String url = Config.BASE_URL + operation;
String body = "accountSid=" + accountSid + "&to=" + to + "&smsContent=" + tmpSmsContent
+ HttpUtil.createCommonParam();
// 提交请求
String result = HttpUtil.post(url, body);
System.out.println("result:" + System.lineSeparator() + result);
}
以上就是短信验证码登录流程详细步骤
作者 | classabcd
如有文章对你有帮助,
欢迎关注❤️、点赞👍、转发📣!
推荐, Java面试题库,详情点击: 牛逼!又发现了一款牛逼的Java面试题库,史上最强! 点击文末“阅读原文”可直达
评论