手机短信验证

------>>关于手机短信发送验证(Spring+SpringMVC+Mybatis框架搭建的)

步骤一:去互亿网站(http://www.ihuyi.com/)申请注册一个帐号密码(也可以去其他网站申请帐号密码)
步骤二:将手机短信接口(webservice)所需要的jar包放进lib下面,有"小奶瓶"出现即可
1、commons-logging-1.0.4.jar
2、commons-httpclient-3.0-rc4.jar
3、commons-codec-1.3.jar
--连接手机短信服务的jar包
步骤三:调用接口url
1、http://106.ihuyi.cn/webservice/sms.php?method=Submit";
2、Math.random()*9+1)*100000随机生成6位验证码发送到手机上
3、将用户名密码输入进去传值调用(帐号:cf_xxxxxxx 密码:xxxxxxx)
步骤四:在需要用到的输入手机号页面-->获取手机验证码-->成功则去成功页面,否则留在本页面(采用ajax技术)

^_^提醒:必须开启网络,才能获取接收短信。测试帐号:cf_xxxxxxx 密码:xxxxxxx即可(每条短信大概为1毛钱)

一  : 编写实体类

package cn.jbit.sms.entity;

import java.io.Serializable;
import lombok.Getter;
import lombok.Setter;

/**
 * @author 任锯东  SMSMessage(分层)Layering
 * @date 2016-08-17 17:27:25
 */
@SuppressWarnings("serial")
public class SMSMessage implements Serializable{


    private @Getter @Setter String mobile;  //手机号
}


二  : Controller    接收手机号并短信发送接收

package cn.jbit.sms.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 任锯东  SMSMessage(分层)Layering
 * @date 2016-08-17 17:27:25
 */
@Controller
public class SMSMessageController{

    //短信接口地址
    private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";

    /**
     * 功能:获取手机号并接收短信
     */
    @RequestMapping("SMS1")
    @ResponseBody
    public boolean SMS1(HttpServletRequest request,HttpServletResponse response,String mobile){

        HttpClient client = new HttpClient(); 
        PostMethod method = new PostMethod(Url);
        int mobile_code = (int)((Math.random()*9+1)*100000);//随机6位验证码
        request.getSession().setAttribute("mobile_code", mobile_code);
        client.getParams().setContentCharset("UTF-8");
        //application/x-www-form-urlencoded 标准的编码形式
        method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");
        //就是不管内存里是不是已经有这个对象 ,都要创建
        String content = new String("您的验证码是:" + mobile_code + "。请不要把验证码泄露给其他人。"); 
        NameValuePair[] data = {//提交短信
                new NameValuePair("account", "cf_xxxxx"), //注册的帐号
                new NameValuePair("password", "xxxx"), //注册密码可以使用明文密码或使用32位MD5加密
                //new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
                new NameValuePair("mobile", mobile), 
                new NameValuePair("content", content),
        };
        method.setRequestBody(data);
        try {

            client.executeMethod(method);   

            String SubmitResult =method.getResponseBodyAsString();

            //System.out.println(SubmitResult);

            Document doc = DocumentHelper.parseText(SubmitResult); 
            Element root = doc.getRootElement();

            String code = root.elementText("code"); 
            String msg = root.elementText("msg");   
            String smsid = root.elementText("smsid");   

            System.out.println(code);
            System.out.println(msg);
            System.out.println(smsid);

            if("2".equals(code)){
                System.out.println("短信提交成功");
                return true;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        return true;
    }
}


三  : Controller    验证成功

package cn.jbit.sms.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 任锯东
 * @date 2016年8月17日 下午8:12:17
 */
@Controller
public class SMSMessagesController {

    /**
     * 功能:验证码判断
     */
    @RequestMapping("SMS2")
    @ResponseBody
    public boolean SMS2(HttpServletRequest request,HttpServletResponse response,String mcode) throws Exception{

        String mobile_code =  request.getSession().getAttribute("mobile_code").toString();
        if(mobile_code.equals(mcode)){
            System.out.println(mcode+"=="+mobile_code);
            return true;
        }else{
            return false;
        }
    }
    /**
     * 功能:短信发送成功页面
     */
    @RequestMapping("Success")
    public String Success(){
        return "success";
    }
}


四 : SMS.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<link type="text/css" rel="stylesheet"
    href="<%=request.getContextPath()%>/css/index.css" />
<script type="text/javascript"
    src="<%=request.getContextPath()%>/scripts/jquery-1.8.3.js"></script>
<script type="text/javascript">
    var InterValObj; //timer变量,控制时间
    var count = 60; //间隔函数,1秒执行
    var curCount;//当前剩余秒数
    //timer处理函数
    function SetRemainTime() {
            if (curCount == 0) {                
                window.clearInterval(InterValObj);//停止计时器
                $("#zphone").removeAttr("disabled");//启用按钮
                $("#zphone").val("重新发送验证码");
            }
            else {
                curCount--;
                $("#zphone").val("请在" + curCount + "秒内输入验证码");
            }
        }
    function get_mobile_code(){
        //向后台发送处理数据
        $.ajax({
            url:"<%=request.getContextPath()%>/SMS1.action",
            type:"post",
            data:{mobile:$("#mobile").val()},
            dataType:"text",
            success:function(data){
                if(data=="true"){
                    $("#zphone").val("已发送");
                            curCount = count;
                //设置button效果,开始计时
             $("#zphone").attr("disabled", "true");
             $("#zphone").val("请在" + curCount + "秒内输入验证码");
             InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器,1秒执行一次
                }
            }
        });
    };
        function check(){
            $.ajax({
                url:"<%=request.getContextPath()%>/SMS2.action",
                type : "post",
                data : {mcode : $("#mcode").val()},
                success : function(data) {
                data = eval(data);
                if (data == true) {
                    alert("验证码正确");
                    $("#xForm").submit();
                } else if (data == false) {
                    alert("验证码错误");
                }
            }
        });
    };
</script>
</head>
<body>
    <div id="main" class="wrap">
        <div class="shadow">
            <em class="corner lb"></em> <em class="corner rt"></em>
            <div class="box">
                <div class="manage">
                    <form name="tel-forget" action="<%=request.getContextPath()%>/Success.action" method="post" id="xForm">
                        <table class="form">
                            <tr>
                                <td class="field"></td>
                                <td class="field">请输入你的手机号:</td><!-- readonly="readonly" --> 
                                <td><input type="text" class="text" id="mobile" name="mobile" />
                                <span></span></td>
                            </tr>
                            <tr>
                                <td class="field"></td>
                                <td class="field">手机短信验证码:</td>
                                <td><input type="text" class="text" id="mcode"
                                    name="mcode" onblur="check()" placeholder="请输入您的手机短信验证码"
                                    autocomplete="off" />&nbsp;<label class="ui-blue"> <input type="button" class="point" value="免费获取验证码"
                                    id="zphone" name="zphone" onclick="get_mobile_code()" /></label><span></span></td>
                            </tr>
                            <tr>
                                <td class="field"></td>
                                <td class="field"></td>
                                <td><label class="ui-blue"><input type="submit" class="point"
                                        value="提交"> </label>&nbsp; <label class="ui-blue"><a
                                        href="#"><input
                                            type="button" class="point" name="button" value="返回" /> </a> </label>
                                </td>
                            </tr>
                        </table>
                    </form>
                </div>
            </div>
        </div>
        <div class="clear"></div>
    </div>
</body>
</html>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

战神丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值