微信开发笔记(1)——配置微信服务器、验证信息
配置微信服务器消息验证接口
微信开发需要你有自己的服务器才可以;如果没有公网服务器,有内网映射工具也是可以,我用的花生壳,网上比较多,大家可以搜索一下
登录微信mp平台
配置服务器信息
在mp平台上:开发菜单-基本配置中配置服务器
*此处的url配制成你自己服务器的接口,此接口为get方式
*此处的Token和之后会涉及到的access_token不同,这里的token是自己定义的,用来验证消息是不是微信服务器发送过来的
*消息加密密钥随机生成就可以,如果之后选择安全模式则需要此处的密钥
都配置完毕后点击提交验证一下服务器可用性;如果提示配置成功则证明你的服务器可用且代码逻辑正确;如果配置失败根据具体提示检验服务器可用性和代码逻辑问题。
java代码逻辑:验证消息是否为微信服务器发来
微信服务器会请求你配置的服务器url,并携带以下参数:
signature :微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
timestamp 时间戳
nonce 随机数
echostr 随机字符串
验证逻辑:
1)将token、timestamp、nonce三个参数进行字典序排序
2)将三个参数字符串拼接成一个字符串进行sha1加密
3)将自己加密后获得的字符串可与signature对比,如果与微信服务器返回的加密字符串signature一样,那就将微信返回来的echostr 随机字符串原封不懂的返回微信服务器,不对的话返回空串或者什么都不返回
java代码示例:
public void pushMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
log.info("WeChatController-->pushMessage():start");
// 微信加密签名
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;
}
/**
* 验证签名
*
* @param signature
* @param timestamp
* @param nonce
* @return
*/
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]);
}
MessageDigest md = null;
String tmpStr = null;
try {
md = MessageDigest.getInstance("SHA-1");
byte[] digest = md.digest(content.toString().getBytes());
tmpStr = byteToStr(digest);
} catch (NoSuchAlgorithmException e) {
// TODO: handle exception
}
content = null;
return tmpStr!=null?tmpStr.equals(signature.toUpperCase()):false;
}
/**
* 将字节数组转换为十六进制字符串
*
* @param byteArray
* @return
public static String byteToStr(byte[] byteArray) {
String strDigest ="";
for (int i = 0; i < byteArray.length; i++) {
strDigest +=byteToHexStr(byteArray[i]);
}
return strDigest;
}
编写验证url的post接口
当与微信服务器验证url无误后,微信服务器后将将用户请求的信息以post方式发送到验证url的post接口上,此时就需要你在这个接口中处理剩下的逻辑
微信服务器会将信息以xml的方式放到request中,所以我们需要将流中的xml信息解析出来,获取xml中的数据。
xml信息类似如下:
解析流中的xml代码示例:
public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
// 将解析结果存储在HashMap中
Map<String, String> map = new HashMap<String, String>();
// 从request中取得输入流
InputStream inputStream = request.getInputStream();
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();
// 遍历所有子节点
for (Element e : elementList){
System.out.println("-->e.getName()-->"+e.getName() +"-->e.getText()-->"+e.getText());
map.put(e.getName(), e.getText());
}
// 释放资源
inputStream.close();
inputStream = null;
return map;
}