美文网首页
Springboot 之小程序公众号等资料

Springboot 之小程序公众号等资料

作者: 牵手生活 | 来源:发表于2019-06-17 21:58 被阅读0次

    公众号

    Java微信公众号开发的精彩世界,学习微信公众号开发的相关概念,编辑模式和开发模式应用,以及百度BAE的使用。

    步骤:

    • 准备一个微信公众号
    • 需要一个公网上的url(必须是80端口),或使用外网映射工具,该url验证消息的确来自微信服务器
    • 编写url验证消息的get访问方法(如采用controller)
    • 微信公众号平台设置l验证url的地址,并启用
    • 微信公众号平台--提示:服务配置已启用
    材料准备
    • 一个微信公众号
    • 外网域名(外网映射工具)--用于开发调试

    与微信对象的url需要具备一下条件。(支付宝小程序也差不多是这样的)

    在公网上能够访问
    端口只支持80端口

    私网 公网映射工具

    Ngrok 实现内网穿透教程(Ngrok 和 Sunny-Ngrok )
    内网ip映射到外网--百度经验

    下载地址: https://ngrok.com/  (需要账号,好像需要科学上网)
    国内:http://www.ittun.com/
    使用cmd 到ngrok.exe的目录,命令: ngrok -authtoken 密钥 -subdomain 二级域名(如: young) 端口(8080)
    
    就可以访问:http://young.ngrok.com就可以了。
    
    修改密钥:访问https://ngrok.com/dashboard 可以管理密钥
    
    
    ngrok设置与安装步骤 获取ngrok密钥
    ./ngrok authtoken 63VPpdpCw62yZmmvJS6yB_4hZH65JKbEpZztqiKY66Y
    # ngrok authtoken 你的密钥
    
    image.png
    ./ngrok http 80
    
    映射设置成功

    网络访问成功情况


    网络访问成功

    微信开发平台配置启用


    微信开发平台配置启用
    数据交互原理-开发模式&编辑模式互斥
    开发模式&编辑模式互斥

    开发模式数据交互原理


    开发模式数据交互原理
    微信公众号--开发模式接入--第一步:配置

    填写开发-基础配置
    安全模式需要-EncodingAESKey


    开发-基础配置
    第二步:验证消息的确来自微信服务器

    springboot的微信公众号(三)微信接入验证

    验证消息的确来自微信服务器

    创建WechatPublic的控制器

    
    @Api(tags = "微信接入验证")
    @Controller
    @RequestMapping("wechatTask")
    public class WechatPublic {
    
        @RequestMapping(value="/wx",method={RequestMethod.GET,RequestMethod.POST})
        public void wxLogin(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
            //设置编码,不然接收到的消息乱码
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            String signature = request.getParameter("signature");//微信加密签名
            String timestamp = request.getParameter("timestamp");//时间戳
            String nonce = request.getParameter("nonce");//随机数
            String echostr = request.getParameter("echostr");//随机字符串
            PrintWriter out = null;
            //接入验证
            if (WeiXinUtil.checkSignature(signature, timestamp, nonce)) {  //校验成功,返回随机字符串
                if (echostr != null) {
                    System.out.println(echostr);
                    try {
                        out = response.getWriter();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    out.write(echostr);//验证成功返回的值
                    return;
                }
            }
        }
    
    }
    
    

    工具类WeiXinUtil

    
    public class WeiXinUtil {
    
        static String token = "token2for2young";
        /**
         *
         * @param signature 微信加密签名
         * @param timestamp 时间戳
         * @param nonce 随机数
         * @return
         */
        public static boolean checkSignature(String signature,String timestamp,String nonce){
            String[] str = new String[]{token,timestamp,nonce};
            //排序
            Arrays.sort(str);
            //拼接字符串
            StringBuffer buffer = new StringBuffer();
            for(int i =0 ;i<str.length;i++){
                buffer.append(str[i]);
            }
            //进行sha1加密
            String temp = SHA1.encode(buffer.toString());
            //与微信提供的signature进行匹对
            return signature.equals(temp);
        }
    
    
    
    }
    
    

    SHA1加密工具类

    public final  class SHA1 {
        private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',
                '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    
        /**
         * Takes the raw bytes from the digest and formats them correct.
         *
         * @param bytes the raw bytes from the digest.
         * @return the formatted bytes.
         */
        private static String getFormattedText(byte[] bytes) {
            int len = bytes.length;
            StringBuilder buf = new StringBuilder(len * 2);
            // 把密文转换成十六进制的字符串形式
            for (int j = 0; j < len; j++) {
                buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
                buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
            }
            return buf.toString();
        }
    
        public static String encode(String str) {
            if (str == null) {
                return null;
            }
            try {
                MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
                messageDigest.update(str.getBytes());
                return getFormattedText(messageDigest.digest());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    
    参考资料

    初识Java微信公众号开发-慕课网视频
    springboot的微信公众号(三)微信接入验证


    微信小程序

    SpringBoot+MyBatis搭建迷你小程序-慕课网视频第4章4-1节

    支付宝小程序

    商业级支付宝小程序入门与实战

    相关文章

      网友评论

          本文标题:Springboot 之小程序公众号等资料

          本文链接:https://www.haomeiwen.com/subject/pmctqctx.html