javaweb项目中实现手机短信登录

<1>首先需要注册个第三方的账户,比如秒嘀科技等,然后拿到三个参数值:QUERAY_PATH ACCOUNT_SID AUTH_TOKEN
<2>编写获取验证码类getMessage.java

private static final String QUERAY_PATH="xxxx";
private static final String ACCOUNT_SID="xxx";
private static final String AUTH_TOKEN="xxx";
/**
* @Title: getCode
* @Description: TODO( 发送验证码 )
* @param @param phone
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String getCode(String phone){
String ran = smsCode();
String timestamp = getStamp();
String sig = getMD5(ACCOUNT_SID, AUTH_TOKEN, timestamp);
String tamp = "您的验证码为"+ran+",请于{2}分钟内正确输入,如非本人操作,请忽略此短信。";
OutputStreamWriter out = null;
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(QUERAY_PATH);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setConnectTimeout(5000);
connection.setReadTimeout(10000);
connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
String args = getArgs(ACCOUNT_SID, tamp, phone, timestamp, sig, "JSON");
out.write(args);
out.flush();

br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String temp="";
while ((temp=br.readLine())!=null) {
sb.append(temp);
}
} catch (Exception e) {
e.printStackTrace();
}

JSONObject json = new JSONObject(sb.toString());
String code = json.getString("respCode");
String defaultrespcode = "00000";
if(defaultrespcode.equals(code)){
return ran;
}else{
return code;
}

}
/**
* @Title: getArgs
* @Description: TODO( 参数拼接 )
* @param @param accountSid
* @param @param smsContent
* @param @param to
* @param @param timestamp
* @param @param sig
* @param @param respDataType
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String getArgs(String accountSid,String smsContent,String to,String timestamp,String sig,String respDataType){
return "accountSid="+accountSid+"&smsContent="+smsContent+"&to="+to+"&timestamp="+timestamp+"&sig="+sig+"&respDataType="+respDataType;
}

/**
* @Title: getStamp
* @Description: TODO( 获取时间戳 )
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String getStamp(){
return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
}
/**
* @Title: getMD5
* @Description: TODO(sig签名 )
* @param @param sid
* @param @param token
* @param @param timestamp
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String getMD5(String sid,String token,String timestamp){
StringBuilder sBuilder = new StringBuilder();
String source = sid + token + timestamp;
try {
MessageDigest instance = MessageDigest.getInstance("MD5");
byte[] digest = instance.digest(source.getBytes());
for (byte b : digest) {
String hexString = Integer.toHexString(b&0xff);
if(hexString.length()==1){
sBuilder.append("0"+hexString);
}else{
sBuilder.append(hexString);
}
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sBuilder.toString();
}
/**
* @Title: smsCode
* @Description: TODO( 产生验证码)
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String smsCode(){
String random = new Random().nextInt(1000000)+"";
if(random.length()!=6){
return smsCode();
}else{
return random;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<3> 编写servlet

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
String phone = req.getParameter("phone");
String code = GetMessage.getCode(phone);
//手机号登录后
HttpSession session = req.getSession();
PrintWriter out = resp.getWriter();
//检查手机号是否注册过
checkPhoneDao checkPhoneDao = new checkPhoneImpl();
boolean results = checkPhoneDao.checkPhone(phone);
if(!results){
out.print(code);
session.setAttribute("name", "phone");
}else {
out.print("此手机号没有被注册");
}
out.close();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<4> dao 层实现 的方法

@Override
public boolean checkPhone(String phone) {
String sql = "select username from user where phone=?";
List<Map<String, Object>> queryForList = DbUtil.queryForList(sql, phone);
if(queryForList.isEmpty()){
return true;
}
return false;
}

posted @ 2019-09-10 17:07  水至清明  阅读(971)  评论(0编辑  收藏  举报