Java微信公众号开发(二)与微信后台对接

需要的jar包

  • dom4j-1.6.1.jar
  • servlet-api.jar
  • xstream-1.3.1.jar

创建servlet类

doGet方法用于验证消息的确来自微信服务器
  • 获取这个五个参数
    signature、timestamp、timestamp、nonce、echostr
  • 调用CheckUtil类里的checkSignature()方法进行验证
    此方法写好后直接调用,涉及一些加密算法无需多看
    Java微信公众号开发(二)与微信后台对接_第1张图片

填写服务器配置

  • URL为内网映射工具得到的地址
  • token与代码中的一致

容易出现的问题

  • doGet方法不要继承父类,否则会出现405错误
    Java微信公众号开发(二)与微信后台对接_第2张图片

代码部分

package xyz.quinntian.servlet;
public class WeixinServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // TODO Auto-generated method stub
    //super.doGet(req, resp);
    String signature = req.getParameter("signature");
    System.out.println("signature:"+signature);
    String timestamp = req.getParameter("timestamp");
    System.out.println("timestamp:"+timestamp);
    String nonce = req.getParameter("nonce");
    System.out.println("nonce:"+nonce);
    String echostr = req.getParameter("echostr");
    System.out.println("echostr:"+echostr);
    PrintWriter out =resp.getWriter();
    if (CheckUitl.checkSignature(signature, timestamp, nonce)) {
        System.err.println("验证成功");
        out.println(echostr);
    }else {
        System.out.println("验证失败");
    }
}

工具类

package xyz.quinntian.Uitl;

import java.security.MessageDigest;
import java.util.Arrays;

public class CheckUitl {
    private static final String token = "quinn";

    public static boolean checkSignature(String signature, String timestamp, String nonce) {
        String[] arr = new String[] { token, timestamp, nonce };
        // 排序
        Arrays.sort(arr);
        // 生成字符串
        StringBuffer content = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            content.append(arr[i]);
        }
        // sha加密
        String temp = getSha1(content.toString());
        System.out.println(signature);
        System.out.println(temp);
        return temp.equals(signature);

    }

    public static String getSha1(String str) {
        if (str == null || str.length() == 0) {
            return null;
        }
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));
            byte[] md = mdTemp.digest();
            int j = md.length;
            char buf[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];

            }
            return new String(buf);
        } catch (Exception e) {
            return null;
        }
    }
}

你可能感兴趣的:(微信公众号开发)