zoukankan      html  css  js  c++  java
  • java对接微信公众号

    对接微信公众号一般我们有测试地址和正式的微信公众号,如果没有正式的建议使用测试地址

    微信公众平台测试地址:https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index

    正式的需要自己开通微信公众号;

    1,登录微信公众平台,设置
    找到左侧菜单中的开发基本配置

     基本配置要求

      1,白名单这个需要记住配置,否则获取不到token, 

        获取白名单IP方式, 浏览器中【百度】直接输入ip,找到显示出ip的这个,把这个写入到白名单中就行

    2,服务器地址 这个是自己项目的地址,一般加上一个方法名称,主要为了验证微信发送的消息

    3,token 任意写【按照微信要求】

    4,EncodingAESKey 点击生成就行

    5,消息加解密方式 一般就选择【明文】 【兼容模式】

    问题:微信公众号配置URL 请求URL超时

    思路:这个可能是我们使用的ip地址是内网的地址, 我在家使用的是路由器提供的ip地址,那这个地址是内网的。总是不能让微信通过域名把信息反给我们。
    解决: 通过natapp这个软件,我们可以得到一个免费的映射。
    地址:https://natapp.cn/#download
    下载软件按照官网文档操作

    mac启动后会出现界面,使用出现的域名作为微信公众号配置的地址,然后加上自己的方法就可以。

    微信配置

    剩下的就是我们的controller中,主要注意点

    1,返回的是字符串

    2,返回的是微信给我门的echostr字符串,验证方式可以到微信官网获取

    地址:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html  

    https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Message_Encryption/Message_encryption_and_decryption.html

    上边两个地址都可以找到下载的代码,只是比较不好找, [官网上给出的demo,我这里总是出问题,不知道怎么调试。就往上找了一个,贴到下边了]

     如果不验真,直接返回微信给的echostr就可以

    @ResponseBody
    @RequestMapping("verify")
    public String verifyUrl(String signature, String timestamp, String nonce, String echostr)
    {
    final boolean b = WeixinCheckoutUtil.checkSignature(signature, timestamp, nonce);
    if(b)
    {
    return echostr;
    }
    return "0";
    }
     
    public class WeixinCheckoutUtil {
    
        // 与接口配置信息中的Token要一致
        private static String token = "ddrsgz";
    
        /**
         * 验证签名
         *
         * @param signature
         * @param timestamp
         * @param nonce
         * @return
         */
        public static boolean checkSignature(String signature, String timestamp, String nonce) {
            String[] arr = new String[] { token, timestamp, nonce };
            // 将token、timestamp、nonce三个参数进行字典序排序
            // Arrays.sort(arr);
            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;
        }
        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;
                    }
                }
            }
        }
    
    
        public static void main(String[] args)
        {
    
            String verifyMsgSig = "70023ded8e972680e8506161043feaeb1ae6d7c8";
            String timeStamp = "1609253670";
            String nonce = "584987070";
            String echoStr = "160542013663168510";
            final boolean b = checkSignature(verifyMsgSig, timeStamp, nonce);
            System.out.println(b);
        }
    }
  • 相关阅读:
    Flask、Vue部署总结
    Recat教程(二)
    C第一个程序
    React教程(一)
    Flask+uwsgi+nginx 部署
    使用Dockerfile部署简单的应用
    Typescript搭建开发环境
    Typescript模块化/命名空间/装饰器
    Typescript的泛型/泛型接口
    修改git密码后,终端fatal: Authentication failed for'http://xxx/xxx.git'
  • 原文地址:https://www.cnblogs.com/renjianjun/p/14193401.html
Copyright © 2011-2022 走看看