Skip to content

企业号建立连接

borball edited this page Dec 28, 2016 · 6 revisions

企业号建立连接

回调模式

@RequestMapping("/wx/qy")
@ResponseBody
public String qy(@RequestParam(value="msg_signature") String signature,
                       @RequestParam(value="timestamp") String timestamp,
                       @RequestParam(value="nonce") String nonce,
                       @RequestParam(value="echostr", required = false) String echostr,
                       @RequestBody(required = false) String content) {

    logger.info("msg_signature={}, nonce={}, timestamp={}, echostr={}", signature, nonce, timestamp, echostr);

    CorpSetting corpSetting = CorpSetting.defaultSettings();

    try {
        MessageDecryption messageDecryption = new MessageDecryption(token, aesKey, corpSetting.getCorpId());
        if (!StringUtils.isEmpty(echostr)) {
            String echo = messageDecryption.decryptEcho(signature, timestamp, nonce, echostr);
            logger.info("消息签名验证成功.");
            return echo;
        } else {
            XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(messageDecryption.decrypt(signature, timestamp, nonce, content));
            XmlMessageHeader xmlResponse = qyDispatch(xmlRequest);
            if(xmlResponse != null) {
                try {
                    return messageDecryption.encrypt(QyXmlMessages.toXml(xmlResponse), timestamp, nonce);
                } catch (WxRuntimeException e) {
                }
            }
        }
    } catch (Exception e) {
        logger.error("callback failed.", e);
    }

    return "";
}

微信官方文档:回调模式

企业号 demo