微信公众号自定义回调开发

配置服务器信息

公众号自定义服务器配置
URL是后台回调接口地址,Token是为了验证回调信息的合法性的,EncodingAESKey随机生成即可

开发公众号配置校验回调开发

配置校验回调是以GET方式回调的,同消息事件回调是一个地址,只是请求方式不同。
如果验证成功,则响应回调请求的echostr值,否则啥也不返回。
代码示例:

/**
 * 微信公众号配置校验
 *
 * @param appId
 * @param request
 * @param response
 */
@RequestMapping(value = "/webchat/{appId}", method = {RequestMethod.GET})
@ApiOperation(value="微信公众号配置校验回调", notes="微信公众号配置校验回调")
public void receiveOfficialMsgGet(@PathVariable("appId") String appId, HttpServletRequest request, HttpServletResponse response) {
    logger.info("收到微信回调GET:{}", appId);

    // 微信加密签名
    String signature = request.getParameter("signature");
    // 时间戳
    String timestamp = request.getParameter("timestamp");
    // 随机数
    String nonce = request.getParameter("nonce");
    // 随机字符串
    String echostr = request.getParameter("echostr");

    Map<String, String[]> map = request.getParameterMap();
    for (Map.Entry<String, String[]> entry : map.entrySet()) {
        logger.info("{}={}", entry.getKey(), entry.getValue() != null ? entry.getValue()[0] : "");
    }

    // 获取公众号信息
    SingleParamRequest<String> singleParamRequest = new SingleParamRequest<>();
    singleParamRequest.setData(appId);
    ServiceJsonBean<OfficialAccount> officialAccountJsonBean = weiXinOfficialAccountService.getOfficialAccountByAppId(singleParamRequest);
    logger.info("获取到微信公众号信息:{}", JSONObject.toJSONString(officialAccountJsonBean));
    String officialToken = "";
    if (officialAccountJsonBean.getCode() == ErrorCode.SUCCESS.getCode() && officialAccountJsonBean.getData() != null) {
        officialToken = officialAccountJsonBean.getData().getOfficialToken();
    }

    String curSign = SecurityUtils.encryptSHA1(officialToken, timestamp, nonce);
    if (StringUtils.isEquals(signature, curSign) && StringUtils.isNotEmpty(echostr)) {
        logger.info("验证成功!");
        try {
            response.getWriter().write(echostr);
        } catch (IOException e) {
            logger.error("response输出异常", e);
        }
    }
}

消息、事件回调

消息、事件回调跟校验回调是同一个接口地址,但是请求方式为POST
消息、事件会以xml格式的传输到后台,后台解析xml进行处理

/**
 * 接收微信公众号消息或事件
 *
 * @param appId
 * @param request
 * @return
 */
@RequestMapping(value = "/webchat/{appId}", method = {RequestMethod.POST})
@ResponseBody
@ApiOperation(value="微信公众号消息或事件回调", notes="微信公众号消息或事件回调")
public void receiveOfficialMsgPost(@PathVariable("appId") String appId, HttpServletRequest request) {
    logger.info("收到微信回调POST:{}", appId);

    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
        String line = "";
        StringBuffer buffer = new StringBuffer();
        while ((line = br.readLine()) != null) {
            buffer.append(line);
        }
        String notifyXml = buffer.toString();
        logger.info("收到微信回调:{}", notifyXml);

        Map<String, String> xmlMap = XmlUtils.xmlToMap(notifyXml);

        // 发布事件
        NormalMsgEvent normalMsgEvent = new NormalMsgEvent(applicationContext, xmlMap, appId);
        applicationContext.publishEvent(normalMsgEvent);
    } catch (Exception e) {
        logger.error("微信消息回调异常", e);
    }
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

来了就走下去

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

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

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

打赏作者

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

抵扣说明:

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

余额充值