在使用前阅读官方文档:回调配置文档 一、配置回调服务 一、在企业微信管理后台配置三个配置 分别是:URL, Token, EncodingAESKey。打开企业微信后台-->管理工具-->通讯录同步配置回调地址如下所示 1.1、URL
在使用前阅读官方文档:回调配置文档
分别是:URL, Token, EncodingAESKey。打开企业微信后台-->管理工具-->通讯录同步配置回调地址如下所示
1.1、URL为回调服务地址,由开发者搭建(直白点就是后台回调域名地址,你给企业微信的URL,例如http://127.0.0.1:8085/qiyewx/getCallBack)服务器如果是云服务要配置域名
2、Token用于计算签名,由英文或数字组成且长度不超过32位的自定义字符串。(可随机获取,但要复制到本地代码,后面会用到)
3、EncodingAESKey用于消息内容加密,由英文或数字组成且长度为43位的自定义字符串。(可随机获取,但要复制到本地代码,后面会用到)
# 企业微信配置qiyewx: url: corpid: corpsecret: #回调配置 token: XXXXXX encodingAESKey: XXXXXX
有JSON版本和xml版本
json版本:
xml版本:
commons-codec commons-codec org.json json 20200518 dom4j dom4j 1.6.1
切记:将将下载的示例代码复制到你的项目代码中,下面的代码中有使用。
@RequestMapping(value = "/getCallBack", method = {RequestMethod.GET, RequestMethod.POST}) public Object CompanyWeChatChangeNotice(httpservletRequest request, @RequestBody(required = false) String body) { Map parameterMap = request.getParameterMap(); String jsonString = JSONObject.toJSONString(parameterMap); log.info("企业微信回调参数:{}, 解析参数:{}", jsonString, body); if (body == null) { Object result = qyWxService.verificationUrl(request); return result; } Map resultMap = qyWxService.getRequestParameter(request, body); System.err.println(resultMap); return "success"; }
public Object verificationUrl(HttpServletRequest request) { log.info("=========验证URL有效性开始========="); String sEchoStr; //需要返回的明文 try { WXBizMsGCrypt wxcpt = new WXBizMsgCrypt(qyWxConfig.getToken(), qyWxConfig.getEncodingAESKey(), qyWxConfig.getCorpid()); String msgSignature = request.getParameter("msg_signature"); String timeStamp = request.getParameter("timestamp"); String nonce = request.getParameter("nonce"); String echostr = request.getParameter("echostr"); log.info("企业微信加密签名: {},时间戳: {},随机数: {},加密的字符串: {}", msgSignature, timeStamp, nonce, echostr); sEchoStr = wxcpt.VerifyURL(msgSignature, timeStamp, nonce, echostr); log.info("给企业微信返回的明文,{}", sEchoStr); log.info("=========验证URL有效性结束========="); return sEchoStr; } catch (AesException e) { log.error("验证URL失败,错误原因请查看异常:{}", e.getCode()); throw new AesException(e.getCode()); } }
public Map getRequestParameter(HttpServletRequest request, String body) { log.info("=========参数解析开始========="); try { WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(qyWxConfig.getToken(), qyWxConfig.getEncodingAESKey(), qyWxConfig.getCorpid()); String msgSignature = request.getParameter("msg_signature"); String timeStamp = request.getParameter("timestamp"); String nonce = request.getParameter("nonce"); log.info("企业微信加密签名: {},时间戳: {},随机数: {}", msgSignature, timeStamp, nonce); String sMsg = wxcpt.DecryptMsg(msgSignature, timeStamp, nonce, body); Map resultMap = new HashMap(16); resultMap = ConstantUtil.parseXmlToMap(sMsg, resultMap); log.info("decrypt密文转为map结果为{}", resultMap); log.info("=========参数解析结束========="); return resultMap; } catch (AesException e) { log.error("密文参数解析失败,错误原因请查看异常:{}", e.getMessage()); throw new AesException(e.getCode()); } }
package com.ruoyi.system.qiwechat.utils;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader;import java.io.StringReader;import java.util.Iterator;import java.util.Map;public class ConstantUtil { public static Map parseXmlToMap(String xml, Map map) { try { SAXReader reader = new SAXReader(); Document doc = reader.read(new StringReader(xml)); Element root = doc.getRootElement(); String path = ""; if (map.containsKey(root.getName().trim())) { path = map.get(root.getName().trim()); map.remove(root.getName().trim()); } for (Iterator i = root.elementIterator(); i.hasNext();) { Element element = (Element) i.next(); if (element.isTextOnly()) { if (path.length() > 0) { map.put(path + element.getName().trim(), element.getTextTrim()); } else { map.put(element.getName().trim(), element.getTextTrim()); } } else { map.put(element.getName().trim(), path+ element.getName().trim() + "."); parseXmlToMap(element.asXML(), map); } } } catch (Exception e) { e.printStackTrace(); } return map; }}
package com.ruoyi.common.config;import lombok.Data;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Configuration;@Data@Configurationpublic class QyWxConfig { @Value("${qiyewx.url}") private String url; @Value("${qiyewx.corpid}") private String corpid; @Value("${qiyewx.corpsecret}") private String corpsecret; @Value("${qiyewx.token}") private String token; @Value("${qiyewx.encodingAESKey}") private String encodingAESKey;}
所要参数依次写入,测试
如下红框所示:
备注:要根据事件的类型Event判断回调的事件,在回调事件中根据ChangeType判断事件性质
看完有所帮助请点点赞,送人玫瑰,手留余香。给作者一个👍🏻
来源地址:https://blog.csdn.net/LIU_ZHAO_YANG/article/details/129137077
--结束END--
本文标题: Java实现企业微信回调配置
本文链接: https://www.lsjlt.com/news/387668.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0