赞
踩
注册账号并授权 ./ngrok authtoken YOURTOKEN
如果不注册就会过期 8h之后生成的链接即会失效
本地端口内网穿透$ ./ngrok http 8553
访问 http://127.0.0.1:4040
重新启动之后更新域名 可$ ./ngrok http 8553 -subdomain 4c4e91f7
如何使用ngrok生成固定的URL中提到也可以使用Serveo $ ssh -R youruniquesubdomain:80:localhost:8000 serveo.net
// 1. 校验消息推送token // /v1/wx/customer_message? // signature=***& // echostr=***& // timestamp=***& // nonce=165725**** @RequestMapping(value = "customer_message", method = RequestMethod.GET, produces = "application/json;charset=utf-8") public String customerMessage(HttpServletRequest request, @RequestParam(required = false, defaultValue = "") String signature, @RequestParam(required = false, defaultValue = "") String timestamp, @RequestParam(required = false, defaultValue = "") String nonce, @RequestParam(required = false, defaultValue = "") String echostr){ // first 校验服务器地址UR if (StringUtils.hasText(echostr)) { boolean tokenOk = false; try{ tokenOk = checkWxToken(signature, timestamp, nonce); } catch (Exception e){ e.printStackTrace(); } if (tokenOk) { // 返回echostr 即可 表示接口校验通过 return echostr; } } return null; } private boolean checkWxToken(String signature, String timestamp, String nonce) throws Exception{ if (!StringUtils.hasText(signature) || !StringUtils.hasText(timestamp) || !StringUtils.hasText(nonce)) { return false; } String token = "123abcdef"; String[] arr = {token, timestamp, nonce}; Arrays.sort(arr); String strTmp = String.join("", arr); strTmp = toSha1(strTmp); if (signature.equals(strTmp)){ return true; } return false; } private String toSha1(String str) throws Exception{ String sha1 = ""; try { MessageDigest crypt = MessageDigest.getInstance("SHA-1"); crypt.reset(); crypt.update(str.getBytes("UTF-8")); sha1 = byteToHex(crypt.digest()); } catch(NoSuchAlgorithmException e) { e.printStackTrace(); } catch(UnsupportedEncodingException e) { e.printStackTrace(); } return sha1; } private static String byteToHex(final byte[] hash){ Formatter formatter = new Formatter(); for (byte b : hash) { formatter.format("%02x", b); } String result = formatter.toString(); formatter.close(); return result; } ...
// "Content" -> "1" // "CreateTime" -> {Integer@9175} 1576062970 // "ToUserName" -> "***" // "FromUserName" -> "***" //发送者openid // "MsgType" -> "text" // "MsgId" -> {Long@9183} 22563797906949896 @RequestMapping(value = "customer_message", method = RequestMethod.POST, produces = "application/json;charset=utf-8") @RequestMapping(value = "customer_message", method = RequestMethod.POST, produces = "application/json;charset=utf-8") public String sendCustomerMessage(HttpServletRequest request, @RequestBody JSONObject body) { System.out.println(body); String msgType = body.getString("MsgType"); String fromUserName = body.getString("FromUserName"); if ("event".equals(msgType) && "user_enter_tempsession".equals(body.getString("Event"))){ JSONObject parameter = new JSONObject(); parameter.put("touser", fromUserName); parameter.put("msgtype", "text"); JSONObject content = new JSONObject(); content.put("content", "请联系客服"); parameter.put("text", content); wxService.customSend(parameter); } else if ("text".equals(msgType) && "1".equals(body.getString("Content"))) { JSONObject parameter = new JSONObject(); parameter.put("touser", fromUserName); parameter.put("msgtype", "link"); JSONObject link = new JSONObject(); link.put("title", "title"); link.put("description", "desc"); link.put("url", "https://www.baidu.com"); link.put("thumb_url", "http://www.baidu.com/100.png"); parameter.put("link", link); wxService.customSend(parameter); } else { // 开启客服消息 JSONObject result = new JSONObject(); result.put("ToUserName", body.getString("FromUserName")); result.put("FromUserName", body.getString("ToUserName")); result.put("CreateTime", body.getLong("CreateTime")); result.put("MsgType", "transfer_customer_service"); System.out.println(result.toString()); return result.toString(); } return ""; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。