微信第三方平台代小程序接收与解密消息

先配置好第三方平台的消息相关配置

注意消息报文中的appid都是通知给第三方平台的appid

需要在接收消息的接口中拼接/$APPID$/ 便可获取到是这条消息给哪个小程序的

下载微信官方消息加解密源码 下载

打开Java项目

将圈出来的拉到你的项目中

@RequestMapping("/appNotify")
public class AppNotifyController {
    
    /**
     * 这里的路径需要与配置的路径相同
     */
    @RequestMapping("/{appId}")
    public String eventMsg(@PathVariable String appId, HttpServletRequest request) throws AesException, IOException {
        // 这里需要通过request获取到原文再进行解密
        StringBuffer sb = new StringBuffer();
        try (ServletInputStream inputStream = request.getInputStream();
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        ) {
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String requestBody = sb.toString();
        // 初始化微信加解密类
        WXBizMsgCrypt WXBizMsgCrypt = new WXBizMsgCrypt(WxConfig.msgToken, WxConfig.msgKey, WxConfig.templateAppId);
        // 提取出xml数据包中的加密消息
        Object[] encrypt = XMLParse.extract(requestBody);
        // 对密文进行解密,拿到解密后的消息字符串
        String decrypt = WXBizMsgCrypt.decrypt(encrypt[1].toString());
    }

}

最后通过event来区分消息事件类型,某些消息是通过MsgType区分的,注意查看文档中消息说明

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专业填坑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值