微信公众开发接入服务器的接口配置信息
微信官方验证主要参数
String signature = request.getParameter("signature");
String echostr = request.getParameter("echostr");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
echostr,如果此参数为空:正常处理微信消息即可,反之为验证。
下面来看一下主要代码:
// 微信消息处理流程 if (null == echostr || echostr.isEmpty()) { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); xml = WeChatUtils.readStreamParameter(request.getInputStream()); if (null != xml && !xml.isEmpty()) { if (null != db) { result = WeChatUtils.processWechatMag(xml, db.getAttention_reply()); response.getWriter().print(result); response.getWriter().flush(); response.getWriter().close(); } else { result = WeChatUtils.processWechatMag(xml, attention_reply); response.getWriter().print(result); response.getWriter().flush(); response.getWriter().close(); } } else { response.getWriter().print("error"); response.getWriter().flush(); response.getWriter().close(); } } // 微信验证处理流程 else { if (null != db) { if (WeChatUtils.checkSignature(signature, timestamp, nonce, db.getWeixin_token())) { response.getWriter().print(echostr); response.getWriter().flush(); response.getWriter().close(); } else { response.getWriter().print("error"); response.getWriter().flush(); response.getWriter().close(); } } else { response.getWriter().print("error"); response.getWriter().flush(); response.getWriter().close(); } }