前段時間自己做的一個小項目中,涉及到用短信驗證碼登錄、注冊的問題,之前沒涉及過這一塊,看了別人的博客其實也是似懂非懂的,現在就將自己做的利用第三方短信平臺來發送驗證碼這個功能記下來。
本文以注冊為例,在SpringMVC+Spring+Mybatis框架的基礎上完成該短信驗證碼功能。
發送短信驗證碼的原理是:隨機生成一個6位數字,將該6位數字保存到session當中,客戶端通過sessionid判斷對應的session,用戶輸入的驗證碼再與session記錄的驗證碼進行比較。
為了防止有廣告嫌疑這里就不說短信平臺是哪個了。
一般的第三方短信平臺都會有他們自己的短信接口,只要讀懂他們的接口稍作稍作改變就能滿足自己的需求。
首先將短信平臺接口代碼列出:這里要下載三個jar包commons-logging-1.1.1.jar,commons-httpclient-3.1.jar,commons-codec-1.4.jar
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
|
import java.io.UnsupportedEncodingException; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; public class SendMsg_webchinese { public static void main(String[] args) throws Exception { HttpClient client = new HttpClient(); post.addRequestHeader( "Content-Type" , "application/x-www-form-urlencoded;charset=gbk" ); //在頭文件中設置轉碼 NameValuePair[] data ={ new NameValuePair( "Uid" , "本站用戶名" ), new NameValuePair( "Key" , "接口安全秘鑰" ), new NameValuePair( "smsMob" , "手機號碼" ), new NameValuePair( "smsText" , "驗證碼:8888" )}; post.setRequestBody(data); client.executeMethod(post); Header[] headers = post.getResponseHeaders(); int statusCode = post.getStatusCode(); System.out.println( "statusCode:" +statusCode); for (Header h : headers) { System.out.println(h.toString()); } String result = new String(post.getResponseBodyAsString().getBytes( "gbk" )); System.out.println(result); //打印返回消息狀態 post.releaseConnection(); } } |
不難看出,我們想要發送的信息是在這行代碼里面:NameValuePair[] data ={ new NameValuePair("Uid", "本站用戶名"),new NameValuePair("Key", "接口安全秘鑰"),new NameValuePair("smsMob","手機號碼"),new NameValuePair("smsText","驗證碼:8888")};
該接口中還有一個result信息,它的作用是告訴用戶短信發送的狀態,1表示發送成功,其他的小于0的為失敗,這里只要知道1是成功即可。
我們實際的操作中,驗證碼肯定是要我們自己生成的。將result信息與驗證碼一起得到,于是很容易想到用一個HashMap集合。下面是以項目自己的需求對接口的更改:
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
|
import java.util.HashMap; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; import com.yuetile.utils.VerifyingCodeGenerator; public class SendMsg_webchineseController { public static HashMap<String,String> getMessageStatus(String phone) throws Exception{ HashMap<String,String> m= new HashMap<String,String>(); HttpClient client = new HttpClient(); post.addRequestHeader( "Content-Type" , "application/x-www-form-urlencoded;charset=gbk" ); //在頭文件中設置轉碼 String code=VerifyingCodeGenerator.generate(); //驗證碼 NameValuePair[] data ={ new NameValuePair( "Uid" , "****" ), new NameValuePair( "Key" , "******" ), new NameValuePair( "smsMob" ,phone), new NameValuePair( "smsText" , "您正在注冊本站會員,本次驗證碼為:" +code+ "" + "有效時間為5分鐘" )}; m.put( "code" , code); post.setRequestBody(data); client.executeMethod(post); Header[] headers = post.getResponseHeaders(); int statusCode = post.getStatusCode(); System.out.println( "statusCode:" +statusCode); for (Header h : headers) { System.out.println(h.toString()); } String result = new String(post.getResponseBodyAsString().getBytes( "gbk" )); System.out.println(result); //打印返回消息狀態 m.put( "result" , result); post.releaseConnection(); return m; } } |
***表示的是在第三方平臺注冊的賬號密碼。
ACTION層:
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
|
/** * @author hang * @Decription 注冊,發送短信驗證碼,保存到Session中 * @param 封裝客戶端請求 POST * @return 返回狀態參數 * @throws Exception */ @ResponseBody @RequestMapping (value = UrlDefine.Register.CHECKMESSAGEWORK, method = RequestMethod.POST) public Object SendCheckMessage(HttpServletRequest request, @RequestBody UserBean u) throws Exception { String message = "發送成功" ; String phone=u.getTelephone(); //獲取到客戶端發來的手機號 UserBean user = userService.getByPhone(phone); if (user != null ) { message = "該手機號已被注冊" ; return new Response(Status.ERROR, message); } else { HashMap<String, String> m = SendMsg_webchineseController.getMessageStatus(phone); //應用發送短信接口 String result = m.get( "result" ); //獲取到result值 if (result.trim().equals( "1" )) { //如果為1,表示成功發送 String code = m.get( "code" ); //獲取發送的驗證碼內容 logger.info( "發送的驗證碼:" +code); //打印日志 HttpSession session = request.getSession(); //設置session session.setAttribute( "code" , code); //將短信驗證碼放到session中保存 session.setMaxInactiveInterval( 60 * 5 ); //保存時間 暫時設定為5分鐘 return new Response(Status.SUCCESS, message); } else { message = "短信發送失敗" ; return new Response(Status.ERROR, message); } } } |
這樣就能發送成功了。
測試:
利用POSTMAN在本地進行測試:
結果:
到此發送成功。
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!
原文鏈接:http://www.cnblogs.com/xkyzp/p/6203845.html