发送短信验证码的原理

本文以注册为例,在SpringMVC+Spring+Mybatis框架的基础上完成该短信验证码功能。

发送短信验证码的原理是:随机生成一个6位数字,将该6位数字保存到session当中,客户端通过sessionid判断对应的session,用户输入的验证码再与session记录的验证码进行比较。

为了防止有广告嫌疑这里就不说短信平台是哪个了。

一般的第三方短信平台都会有他们自己的短信接口,只要读懂他们的接口稍作稍作改变就能满足自己的需求。

首先将短信平台接口代码列出:这里要下载三个jar包commons-logging-1.1.1.jarcommons-httpclient-3.1.jarcommons-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();
PostMethod post = new PostMethod( "http://gbk.sms.webchinese.cn" ); //该第三方短信服务地址
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();
PostMethod post = new PostMethod( "http://gbk.sms.webchinese.cn" );
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

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值