/** * 请求校验工具类 */ public class SignUtil { // 与接口配置信息中的Token要一致,我的是明文格式 private static String token = "填写你服务器配置时写的token"; public static boolean checkSignature(String signature, String timestamp, String nonce) { //从请求中(也就是微信服务器传过来的)拿到的token, timestamp, nonce String[] arr = new String[] { token, timestamp, nonce }; // 将token、timestamp、nonce三个参数进行字典序排序 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; } //用于字典排序 public static void sort(String a[]) { for (int i = 0; i < a.length - 1; i++) { for (int j = i + 1; j < a.length; j++) { if (a[j].compareTo(a[i]) < 0) { String temp = a[i]; a[i] = a[j]; a[j] = temp; } } } } } //事件响应的接口 @RequestMapping(value="/GZHConcern.do") public void GZHConcern(HttpServletRequest request, HttpServletResponse response) throws IOException { String message = "success"; // 微信加密签名 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); //在这里相应微信的操作 } try { Map<String, String> map = XmlUtil.xmlToMap(request); String fromUserName = map.get("FromUserName");//消息来源用户标识 String toUserName = map.get("ToUserName");//消息目的用户标识 String msgType = map.get("MsgType");//消息类型 String content = map.get("Content");//消息内容 String eventType = map.get("Event"); WeixinUser weixinUser = new WeixinUser(); if(MessageUtil.MSGTYPE_EVENT.equals(msgType)){//如果为事件类型 if(MessageUtil.MESSAGE_SUBSCIBE.equals(eventType)){//处理订阅事件 //获取token String token = WXUtil.getGZHToken(); weixinUser = WXUtil.getUnionid(fromUserName, token); //进行数据库的操作 weixinUser.setNickname(weixinUser.getNickname()); int row = gzhService.addGZHUser(weixinUser); //通过openid获取用户的数据 message = MessageUtil.subscribeForText(toUserName, fromUserName); }else if(MessageUtil.MESSAGE_UNSUBSCIBE.equals(eventType)){//处理取消订阅事件 message = MessageUtil.unsubscribe(toUserName, fromUserName); weixinUser.setOpenid(fromUserName); //进行数据库的操作 int row = gzhService.deleteGZHUser(weixinUser); } } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { out.close(); } out = null; } /* * 消息处理工具类 */ public class MessageUtil { public static final String MSGTYPE_EVENT = "event";//消息类型--事件 public static final String MESSAGE_SUBSCIBE = "subscribe";//消息事件类型--订阅事件 public static final String MESSAGE_UNSUBSCIBE = "unsubscribe";//消息事件类型--取消订阅事件 public static final String MESSAGE_TEXT = "text";//消息类型--文本消息 /* * 组装文本消息 */ public static String textMsg(String toUserName,String fromUserName,String content){ TextMessage text = new TextMessage(); text.setFromUserName(toUserName); text.setToUserName(fromUserName); text.setMsgType(MESSAGE_TEXT); text.setCreateTime(new Date().getTime()); text.setContent(content); return XmlUtil.textMsgToxml(text); } /* * 响应订阅事件--回复文本消息 */ public static String subscribeForText(String toUserName,String fromUserName){ return textMsg(toUserName, fromUserName, "欢迎关注,精彩内容不容错过!!!"); } /* * 响应取消订阅事件 */ public static String unsubscribe(String toUserName,String fromUserName){ //TODO 可以进行取关后的其他后续业务处理 System.out.println("用户:"+ fromUserName +"取消关注~"); return ""; } } /* * xml处理工具类 */ public class XmlUtil { /* * xml转map */ public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException{ HashMap<String, String> map = new HashMap<String,String>(); SAXReader reader = new SAXReader(); InputStream ins = request.getInputStream(); Document doc = reader.read(ins); Element root = doc.getRootElement(); @SuppressWarnings("unchecked") List<Element> list = (List<Element>)root.elements(); for(Element e:list){ map.put(e.getName(), e.getText()); } ins.close(); return map; } /* * 文本消息对象转xml */ public static String textMsgToxml(TextMessage textMessage){ XStream xstream = new XStream(); xstream.alias("xml", textMessage.getClass()); return xstream.toXML(textMessage); } }
微信小程序与公众号实现数据互通的方法(2)
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:http://www.heiqu.com/8d5213a5e35eb7ba38ccffd62df87d9b.html