微信第三方平台授权消息接收与解密

配置好授权事件接收配置地址之后  点击下载  官方消息加解密demo

将圈中的类加入到你的项目中

public class NotifyController {
    // 注意 请求路径要与在开放平台配置的一致
    public String authorize(HttpServletRequest request) throws Exception {
        // 需要通过Reqeust获取原文
        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();
        // 初始化解密类  消息token 消息key 第三方平台appId
        WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(WxConfig.msgToken, WxConfig.msgKey, WxConfig.templateAppId);
        // 提取出xml数据包中的加密消息
        Object[] encrypt = XMLParse.extract(requestBody);
        // 对密文进行解密 拿到解密后的报文 根据报文中的InfoType字段 判断本次小心是哪种类型的
        String decrypt = wxBizMsgCrypt.decrypt(encrypt[1].toString());
    }

}

然后需要对XMLParse.extract(requestBody)这个方法进行修改

 
    public static Object[] extract(String xmltext) throws AesException     {
		Object[] result = new Object[3];
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			StringReader sr = new StringReader(xmltext);
			InputSource is = new InputSource(sr);
			Document document = db.parse(is);

			Element root = document.getDocumentElement();
			NodeList nodelist1 = root.getElementsByTagName("Encrypt");
			NodeList nodelist2 = root.getElementsByTagName("AppId");
			result[0] = 0;
			result[1] = nodelist1.item(0).getTextContent();
			result[2] = nodelist2.item(0).getTextContent();
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			throw new AesException(AesException.ParseXmlError);
		}
	}

后续就可以正常接收第三方应用的消息了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

专业填坑

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

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

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

打赏作者

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

抵扣说明:

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

余额充值