网易云信 实现短信验证码功能

一、步骤

  1. 登录网易云信,创建短信应用。
  2. 申请短信使用业务。
  3. 创建短信验证码模板(验证码模板里有一个默认的模板),也可以自定义创建短信模板。
  4. 创建web项目导入jar包,httpclient.jar和httpcore.jar(网易云信里有自己下)
  5. 一个简单的注册页面。
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>


    注册界面
    


    
用户名:
密码:
手机号:
验证码:
姓名:

6、准备网易云信提供的两个操作类(可以把他们设置为工具类)

需要注意的是APP_KEY和APP_SECRET两个参数,到网易云信的APP_KEY管理中获取这两个参数,更改到代码对应位置。

其中public static String sendCode(String mobile,String templateId)方法传递的参数是手机号和短信模板,返回值是一个字符串{code,msg,obj};

package cn.hgq.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


/**
 * 发送验证码
 * @author liuxuanlin
 *
 */
public class SendCode {
    //发送验证码的请求路径URL
    private static final String
            SERVER_URL="https://api.netease.im/sms/sendcode.action";
    //网易云信分配的账号,请替换你在管理后台应用下申请的Appkey
    private static final String
            APP_KEY="af9db5152817177eb1fe1ea********";
    //网易云信分配的密钥,请替换你在管理后台应用下申请的appSecret
    private static final String APP_SECRET="109a3b697695";
    //随机数
    private static final String NONCE="123456";
    //验证码长度,范围4~10,默认为4
    private static final String CODELEN="6";

    public static String sendCode(String mobile,String templateId) throws Exception {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(SERVER_URL);
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        /*
         * 参考计算CheckSum的java代码,在上述文档的参数列表中,有CheckSum的计算文档示例
         */
        String checkSum = CheckSumBuilder.getCheckSum(APP_SECRET, NONCE, curTime);

        // 设置请求的header
        httpPost.addHeader("AppKey", APP_KEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

        // 设置请求的的参数,requestBody参数
        List nvps = new ArrayList();
        /*
         * 1.如果是模板短信,请注意参数mobile是有s的,详细参数配置请参考“发送模板短信文档”
         * 2.参数格式是jsonArray的格式,例如 "['13888888888','13666666666']"
         * 3.params是根据你模板里面有几个参数,那里面的参数也是jsonArray格式
         */
        nvps.add(new BasicNameValuePair("templateid", templateId));
        nvps.add(new BasicNameValuePair("mobile", mobile));
        nvps.add(new BasicNameValuePair("codeLen", CODELEN));

        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));

        // 执行请求
        HttpResponse response = httpClient.execute(httpPost);
        /*
         * 1.打印执行结果,打印结果一般会200、315、403、404、413、414、500
         * 2.具体的code有问题的可以参考官网的Code状态表
         */
        return EntityUtils.toString(response.getEntity(), "utf-8");
    }
}
package cn.hgq.util;

import java.security.MessageDigest;

public class CheckSumBuilder {
    // 计算并获取CheckSum
    public static String getCheckSum(String appSecret, String nonce, String curTime) {
        return encode("sha1", appSecret + nonce + curTime);
    }

    // 计算并获取md5值
    public static String getMD5(String requestBody) {
        return encode("md5", requestBody);
    }

    private static String encode(String algorithm, String value) {
        if (value == null) {
            return null;
        }
        try {
            MessageDigest messageDigest
                    = MessageDigest.getInstance(algorithm);
            messageDigest.update(value.getBytes());
            return getFormattedText(messageDigest.digest());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    private static String getFormattedText(byte[] bytes) {
        int len = bytes.length;
        StringBuilder buf = new StringBuilder(len * 2);
        for (int j = 0; j < len; j++) {
            buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
            buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
        }
        return buf.toString();
    }
    private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
}

 7、之后就开始写控制器了

package cn.hgq.controller;

import cn.hgq.entity.PhoneCode;
import cn.hgq.entity.User;
import cn.hgq.util.SendCode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class CodeController {
    @RequestMapping("/getCode")
    @ResponseBody
    public PhoneCode getCode(String phone, HttpSession session) throws Exception {
        String result = SendCode.sendCode(phone, "14834232");
        ObjectMapper om = new ObjectMapper();
        PhoneCode phoneCode = om.readValue(result, PhoneCode.class);
        if(phoneCode.getCode()==200){
            session.setAttribute("code",phoneCode.getObj());
            //设置session的最大存活时间为三分钟,也就是验证码有效期在session中3分钟
            session.setMaxInactiveInterval(60*3);
        }
        return phoneCode;
    }
    @RequestMapping("/reg")
    public void register(User user,String code,HttpSession session){
        System.out.println("===================");
        System.out.println(user);
        System.out.println(code);
        System.out.println(session.getAttribute("code"));
        String sessionCode= (String) session.getAttribute("code");
        if (sessionCode==null || !(code.equals(sessionCode))){
            // return "验证码不正确";
        }else {
            //把用户数据存到数据库
        }
    }
}

返回结果对应的实体类

package cn.hgq.entity;

public class PhoneCode {
    private Integer code;
    private String msg;
    private String obj;

    public PhoneCode(Integer code, String msg, String obj) {
        this.code = code;
        this.msg = msg;
        this.obj = obj;
    }

    public PhoneCode() {
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getObj() {
        return obj;
    }

    public void setObj(String obj) {
        this.obj = obj;
    }

    @Override
    public String toString() {
        return "PhoneCode{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", obj='" + obj + '\'' +
                '}';
    }
}

 

你可能感兴趣的:(网易云信 实现短信验证码功能)