refer : https://mp.weixin.qq.com/wiki
第一步:填写服务器配置
注意:填完了之后先不要点确定,先做第二步,做完第二步再点确定。
第二步:验证消息的确来自微信服务器 并返回数据
开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败。加密/校验流程如下:
1)将token、timestamp、nonce三个参数进行字典序排序
2)将三个参数字符串拼接成一个字符串进行sha1加密
3)开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
要想快速通过,不检验也可以,直接返回echostr的内容即可。
附上java的代码:
@RequestMapping("/wx/getMsg.htm")
public ModelAndView mock(String echostr, HttpSession httpSession, HttpServletResponse response,HttpServletRequest request) throws Exception{
if ( ! StringUtil.isNull(request.getParameter("echostr"))) {
PrintWriter writer = response.getWriter();
writer.print(request.getParameter("echostr"));
writer.flush();
writer.close();
return null;
}
System.out.println(request.getRequestURL());
System.out.println(request.getQueryString());
// 取HTTP请求流
ServletInputStream sis = request.getInputStream();
// 取HTTP请求流长度
int size = request.getContentLength();
if (size<0) return null;
// 用于缓存每次读取的数据
byte[] buffer = new byte[size];
// 用于存放结果的数组
byte[] xmldataByte = new byte[size];
int count = 0;
int rbyte = 0;
// 循环读取
while (count < size) {
// 每次实际读取长度存于rbyte中
rbyte = sis.read(buffer);
for(int i=0;i<rbyte;i++) {
xmldataByte[count + i] = buffer[i];
}
count += rbyte;
}
String xmlData = new String(xmldataByte, "UTF-8");
System.out.println("recv from wx ->" + xmlData);
WxTextMsgBean wx = new WxTextMsgBean();
XMLUtil.parserWXRecvMsgxml(xmlData, wx );
String rtnStr = XMLUtil.createWXSendMsgXml(wx, "我就是不告诉你,不服你来打我啊。");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter writer = response.getWriter();
writer.print(rtnStr);
writer.flush();
writer.close();
return null;
}
XML解析相关:
package xm.web.utils;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import xm.web.bean.wx.msg.WxTextMsgBean;
public class XMLUtil {
public static String createWXSendMsgXml(WxTextMsgBean msg, String textMsgContent) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element root = document.createElement("xml");
document.appendChild(root);
// add a node
Element toUserName = document.createElement("ToUserName");
root.appendChild(toUserName);
toUserName.appendChild(document.createCDATASection(msg.getFromUserName()));
Element fromUserName = document.createElement("FromUserName");
root.appendChild(fromUserName);
fromUserName.appendChild(document.createCDATASection(msg.getToUserName()));
Element createTime = document.createElement("CreateTime");
root.appendChild(createTime);
createTime.appendChild(document.createTextNode(String.valueOf(System.currentTimeMillis())));
Element msgType = document.createElement("MsgType");
root.appendChild(msgType);
msgType.appendChild(document.createCDATASection("text"));
Element content = document.createElement("Content");
root.appendChild(content);
content.appendChild(document.createCDATASection(textMsgContent));
String rtn = toStringFromDoc(document);
rtn = rtn.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
System.out.println(rtn);
return rtn;
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
/**
* 解析微信发来的【文本】消息
* @param xmlStr 字符串xml
* @param wx WxRecvMsgBean 用来存放解析结果
*/
public static void parserWXRecvMsgxml(String xmlStr, WxTextMsgBean wx) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
StringReader sr = new StringReader(xmlStr);
InputSource is = new InputSource(sr);
Document doc = builder.parse(is);
NodeList nodeList=doc.getElementsByTagName("*");
for(int i=0;i<nodeList.getLength();i++){
switch (nodeList.item(i).getNodeName()) {
case "ToUserName":
wx.setToUserName(nodeList.item(i).getTextContent());
break;
case "FromUserName":
wx.setFromUserName(nodeList.item(i).getTextContent());
break;
case "CreateTime":
wx.setCreateTime(nodeList.item(i).getTextContent());
break;
case "MsgType":
wx.setMsgType(nodeList.item(i).getTextContent());
break;
case "Content":
wx.setContent(nodeList.item(i).getTextContent());
break;
case "MsgId":
wx.setMsgId(nodeList.item(i).getTextContent());
break;
default:
break;
}
}
}catch(ParserConfigurationException e){
e.printStackTrace();
}catch (SAXException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
/*
* 把dom文件转换为xml字符串
*/
public static String toStringFromDoc(Document document) {
String result = null;
document.setXmlStandalone(false);
if (document != null) {
StringWriter strWtr = new StringWriter();
StreamResult strResult = new StreamResult(strWtr);
TransformerFactory tfac = TransformerFactory.newInstance();
try {
javax.xml.transform.Transformer t = tfac.newTransformer();
// t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
t.setOutputProperty(OutputKeys.INDENT, "yes");
// t.setOutputProperty(OutputKeys.METHOD, "xml");
// text
// t.setOutputProperty(
// "{http://xml.apache.org/xslt}indent-amount", "4");
// t.clearParameters();
t.transform(new DOMSource(document.getDocumentElement()),
strResult);
} catch (Exception e) {
System.err.println("XML.toString(Document): " + e);
}
result = strResult.getWriter().toString();
try {
strWtr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}
package xm.web.bean.wx.msg;
public class WxTextMsgBean {
private String toUserName;
private String fromUserName;
private String createTime;
private String msgType;
private String content;
private String msgId;
public String getToUserName() {
return toUserName;
}
public void setToUserName(String toUserName) {
this.toUserName = toUserName;
}
public String getFromUserName() {
return fromUserName;
}
public void setFromUserName(String fromUserName) {
this.fromUserName = fromUserName;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getMsgType() {
return msgType;
}
public void setMsgType(String msgType) {
this.msgType = msgType;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getMsgId() {
return msgId;
}
public void setMsgId(String msgId) {
this.msgId = msgId;
}
}
附上调试的链接:
https://mp.weixin.qq.com/debug/