有幸接触到公司项目的微信开发,自己私下注册了一个微信服务号做研究。
接入概述
接入微信公众平台开发,开发者需要按照如下步骤完成:
1、填写服务器配置
2、验证服务器地址的有效性
3、依据接口文档实现业务逻辑
下面介绍服务器配置和接入
在公众号管理的后台选择开发---基本配置 -----点击修改配置
这里的URL需要填写自己的服务器地址,本人自己租用了一个阿里云服务器作测试,这里的URL必须要是域名不能是IP地址。实际上是验证服务的一个流程
需要用代码来实现,提交这个表单之后,微信服务器会发送一个请求到自己填写的服务器地址,里面夹带几个参数 signnature timestamp nonce echostr
自己写一个servlet来接受此请求(本文全部采用java代码实现) 通过本地算法验证 如果校验成功,则原样返回echostr.微信服务器接受到同样的echostr后 提示验证成功
表单里的Token为自定义EncodingAESKey 消息加密自动生成
此方法用来校验服务器是否成功和微信服务器对接.具体可以参考微信开发文档https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319&token=&lang=zh_CN
然后启用配置就可以了 至此你的服务器就和微信服务器对接验证成功了
具体验证代码实现如下
package com.github.controller;
import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.github.constant.Constant;
import com.github.util.HttpRequestUtil;
import com.github.util.MessageUtil;
import com.github.util.SignUtil;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.github.service.CoreService;
import com.github.util.ReturnModel;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.StringUtils;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
/**
* 微信公众号webservice主服务接口,提供与微信服务器的信息交互
*
* @param request
* @param response
* @throws Exception
*/
@RequestMapping(value = "core",method = RequestMethod.GET)
public void check(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 微信加密签名
String signature = request.getParameter("signature");
// 时间戳
String timestamp = request.getParameter("timestamp");
// 随机数
String nonce = request.getParameter("nonce");
// 随机字符串
String echostr = request.getParameter("echostr");
PrintWriter out = response.getWriter();
// 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
if (SignUtil.checkSignature(signature, timestamp, nonce)) {
out.print(echostr);
}
out.close();
out = null;
}
package com.github.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
/**
* Created by my on 2016/11/22.
*/
public class SignUtil {
// 与接口配置信息中的Token要一致
private static String token = "weixinCourse";
public static boolean checkSignature(String signature, String timestamp, String nonce) {
String[] arr = new String[]{token, timestamp, nonce};
Arrays.sort(arr);
StringBuilder content = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
content.append(arr[i]);
}
MessageDigest md = null;
String tmpStr = null;
try {
md = MessageDigest.getInstance("SHA-1");
// 将三个参数字符串拼接成一个字符串进行sha1加密
byte[] digest = md.digest(content.toString().getBytes());
tmpStr = byteToStr(digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
content = null;
// 将sha1加密后的字符串可与signature对比,标识该请求来源于微信
return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
}
private static String byteToStr(byte[] byteArray) {
String strDigest = "";
for (int i = 0; i < byteArray.length; i++) {
strDigest += byteToHexStr(byteArray[i]);
}
return strDigest;
}
private static String byteToHexStr(byte mByte) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C','D', 'E', 'F' };
char[] tempArr = new char[2];
tempArr[0] = Digit[(mByte>>>4) & 0X0F];
tempArr[1] = Digit[mByte & 0X0F];
String s = new String(tempArr);
return s;
}
}
注入我的项目打包之后名字是weixin 所以表单上面请求是http://123.cn/weixin/core token填写的是weixinCourse