Skip to content

Commit a9e96f5

Browse files
committed
JDBC:接入阿里短信验证码 v1.5
1 parent 5f76c3f commit a9e96f5

7 files changed

+90
-22
lines changed

.classpath

+3
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
<classpathentry kind="src" path="src"/>
55
<classpathentry kind="lib" path="lib/zxing.jar" sourcepath="F:/Google/zxing-master.zip"/>
66
<classpathentry kind="lib" path="lib/ojdbc6.jar"/>
7+
<classpathentry kind="lib" path="lib/aliyun-java-sdk-core-4.1.0.jar"/>
8+
<classpathentry kind="lib" path="lib/aliyun-java-sdk-dysmsapi-1.0.0.jar"/>
9+
<classpathentry kind="lib" path="lib/gson-2.8.5.jar"/>
710
<classpathentry kind="output" path="bin"/>
811
</classpath>

lib/aliyun-java-sdk-core-4.1.0.jar

155 KB
Binary file not shown.
20.6 KB
Binary file not shown.

lib/gson-2.8.5.jar

236 KB
Binary file not shown.

src/tv/zhangjia/bike/Test.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
public class Test {
44
public static void main(String[] args) {
5-
5+
System.out.println(20 * 0.5);
66
}
77
}
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package tv.zhangjia.bike.util;
2+
3+
import java.util.Random;
4+
5+
import com.aliyuncs.CommonRequest;
6+
import com.aliyuncs.CommonResponse;
7+
import com.aliyuncs.DefaultAcsClient;
8+
import com.aliyuncs.IAcsClient;
9+
import com.aliyuncs.exceptions.ClientException;
10+
import com.aliyuncs.exceptions.ServerException;
11+
import com.aliyuncs.http.MethodType;
12+
import com.aliyuncs.profile.DefaultProfile;
13+
14+
/*
15+
pom.xml
16+
<dependency>
17+
<groupId>com.aliyun</groupId>
18+
<artifactId>aliyun-java-sdk-core</artifactId>
19+
<version>4.0.3</version>
20+
</dependency>
21+
*/
22+
public class CommonRpc {
23+
public static void main(String[] args) {
24+
new CommonRpc().sendCode("15628791997");
25+
}
26+
27+
public String sendCode(String tel) {
28+
DefaultProfile profile = DefaultProfile.getProfile("default", "xxx",
29+
"xxx");
30+
IAcsClient client = new DefaultAcsClient(profile);
31+
32+
CommonRequest request = new CommonRequest();
33+
request.setMethod(MethodType.POST);
34+
request.setDomain("dysmsapi.aliyuncs.com");
35+
request.setVersion("2017-05-25");
36+
request.setAction("SendSms");
37+
request.putQueryParameter("PhoneNumbers", tel);
38+
request.putQueryParameter("SignName", "xxx");
39+
request.putQueryParameter("TemplateCode", "xxxxx");
40+
String random = String.valueOf(new Random().nextInt(9999) + 100);
41+
request.putQueryParameter("TemplateParam", "{\"code\":\"" + random + "\"}");
42+
try {
43+
CommonResponse response = client.getCommonResponse(request);
44+
System.out.println(response.getData());
45+
} catch (ServerException e) {
46+
e.printStackTrace();
47+
} catch (ClientException e) {
48+
e.printStackTrace();
49+
}
50+
return random;
51+
}
52+
}

src/tv/zhangjia/bike/util/Menu.java

+34-21
Original file line numberDiff line numberDiff line change
@@ -105,33 +105,34 @@ public void mainMenu() {
105105

106106
private int retrievePassword(int userId) {
107107

108+
CommonRpc cr = new CommonRpc();
108109
System.out.print("请输入您的手机号:");
110+
String tel = "";
109111
while (true) {
110-
String tel = input.next();
111-
112-
if (!userDao.isTrueTel(tel, userId)) {
113-
System.out.print("该手机号和您的用户名不匹配,请重新输入:");
112+
tel = input.next();
113+
if (iiv.isTrueTel(tel)) {
114+
// 手机号存在返回true
115+
if (!userDao.isTelExist(tel)) {
116+
System.out.print("该手机号不存在,请重新输入手机号:");
117+
} else {
118+
String code = cr.sendCode(tel);
119+
System.out.print("验证码已发送至您的手机,请输入您的验证码:");
120+
while(true) {
121+
String c = input.next();
122+
if(code.equals(c)) {
123+
System.out.print("验证码输入正确,验证成功!");
124+
break;
125+
} else {
126+
System.out.print("验证码输入有误,请重新输入:");
127+
}
128+
}
129+
break;
130+
}
114131
} else {
115-
break;
132+
System.out.print("手机号不合法,哪有这种手机号啊,请重新输入手机号:");
116133
}
117-
}
118-
String codes = VerificationCode.randomCode();
119-
System.out.print("请输入您的验证码:");
120-
121-
try {
122-
Thread.sleep(1500);//
123-
} catch (InterruptedException e) {
124-
e.printStackTrace();
125-
}
126-
// 没有手机给你发验证码,只能委屈你自己输出了
127-
System.out.print(codes);
128134

129-
try {
130-
Thread.sleep(500);//
131-
} catch (InterruptedException e) {
132-
e.printStackTrace();
133135
}
134-
System.out.println(" 验证码正确!");
135136

136137
System.out.print("请输入新密码:");
137138
String newPassword = input.next();
@@ -1436,6 +1437,7 @@ private void userRegister() {
14361437
}
14371438

14381439
String tel;
1440+
CommonRpc cr = new CommonRpc();
14391441
System.out.print("请输入您的手机号:");
14401442
while (true) {
14411443
tel = input.next();
@@ -1444,6 +1446,17 @@ private void userRegister() {
14441446
if (userDao.isTelExist(tel)) {
14451447
System.out.print("该手机号已经存在,请重新输入手机号:");
14461448
} else {
1449+
String code = cr.sendCode(tel);
1450+
System.out.print("验证码已发送至您的手机,请输入您的验证码:");
1451+
while(true) {
1452+
String c = input.next();
1453+
if(code.equals(c)) {
1454+
System.out.print("验证码输入正确,验证成功!");
1455+
break;
1456+
} else {
1457+
System.out.print("验证码输入有误,请重新输入:");
1458+
}
1459+
}
14471460
break;
14481461
}
14491462
} else {

0 commit comments

Comments
 (0)