E企盈营销工具技术服务商 热线:4006-838-530

小程序配置服务器java代码消息推送配置Token令牌错误校验失败

E企盈直播平台营销卖货系统

配置小程序消息推送。之前一直报 token 错误     小程序配置消息推送,微信服务器会发送验证    @RequestMapping(method = RequestMethod.GET)    @ResponseBody    public void get(HttpServletRequest request, HttpServletResponse response) throws Exception{              // 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。          String signature = request.getParameter(“signature”);          // 时间戳          String timestamp = request.getParameter(“timestamp”);          // 随机数          String nonce = request.getParameter(“nonce”);          // 随机字符串          String echostr = request.getParameter(“echostr”);                        PrintWriter out = null;        try {            out = response.getWriter();            // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,否则接入失败            if (SignUtil.checkSignature(signature, timestamp, nonce)) {                out.print(echostr);              out.flush();   //这个地方必须画重点,消息推送配置Token令牌错误校验失败,搞了我很久,必须要刷新!!!!!!!          }       } catch (IOException e) {            e.printStackTrace();        } finally {            out.close();            out = null;             }      } 验证签名是否正确/**      * 验证签名      * @param signature      * @param timestamp      * @param nonce      * @return      */      public static boolean checkSignature(String signature, String timestamp, String nonce) {          //与token 比较        String[] arr = new String[] { TOKEN, timestamp, nonce };          // 将token、timestamp、nonce三个参数进行字典排序          Arrays.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;      }  使用的工具方法/**      * 将字节数组转换为十六进制字符串      *       * @param byteArray      * @return      */      private static String byteToStr(byte[] byteArray) {          String strDigest = “”;          for (int i = 0; i byteArray.length; i++) {              strDigest += byteToHexStr(byteArray[i]);          }          return strDigest;      }        /**      * 将字节转换为十六进制字符串      *       * @param mByte      * @return      */      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;      }

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 小程序配置服务器java代码消息推送配置Token令牌错误校验失败
分享到: 更多 (0)
E企盈小程序直播营销卖货系统
E企盈直播平台营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏