企业微信中设置回调接口url以及验证 spring boot项目实现

官方文档:

接收消息与事件:

加密解密文档:加解密库下载与返回码 - 文档 - 企业微信开发者中心

下载java样例

加解密库下载与返回码 - 文档 - 企业微信开发者中心

将解压开的代码

'将文件夹:qq\weixin\mp\aes的代码作为工具拷到项目中

pom文件中加入

复制代码
<!--企业微信中用于加密解码-->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>

编写会回调接口

复制代码
/**
 * 企业微信
 */
@RestController
@RequestMapping("/api/qyWx")
public class WxQyController extends BaseController {

    //token
    public final static String TOKEN = "企业微信中的";
    // encodingAESKey
    public final static String ENCODINGAES_KEY = "使用自己生成的企业微信自动生成的有问题测试";
    //企业ID
    public final static String CORP_ID = "XXXXX";


    @GetMapping("/callback")
    public void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 微信加密签名
        String msg_signature = request.getParameter("msg_signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 随机字符串
        String echostr = request.getParameter("echostr");

        System.out.println("request=" + request.getRequestURL());
        System.out.println("msg_signature=" + msg_signature);
        System.out.println("timestamp=" + timestamp);
        System.out.println("nonce=" + nonce);
        System.out.println("echostr=" + echostr);

        PrintWriter out = response.getWriter();
        // 通过检验msg_signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
        String result = null;
        try {
            WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(TOKEN, ENCODINGAES_KEY, CORP_ID);
            result = wxcpt.VerifyURL(msg_signature, timestamp, nonce, echostr);
        } catch (AesException e) {
            e.printStackTrace();
        }
        if (result == null) {
            result = TOKEN;
        }
        out.print(result);
        out.close();
        out = null;
    }

    //自主生成EncodingAESKey
    @RequestMapping("/getEncodingAESKey")
    public String getEncodingAESKey()  {

        /**
         * 主要解决有时候直接使用企业微信那边生成的EncodingAESKey会报错
         * "Last encoded character (before the paddings if any) is a valid base 64 alphabet but not a possible value",
         *
         * 使用 commons-codec 加密 32 位字符(我是用的 UUID 生成 ID 后去除 -)
         * 生成一个 EncodingAESKey 替换微信生成的 EncodingAESKey
         * Base64.encodeBase64String(UUID.randomUUID().toString().replaceAll("-","").getBytes());
         * 第二步得到一个 44 位字符串,需要去除末尾等号得到 43 位EncodingAESKey,再去企业微信配置此EncodingAESKey
         */
        String str = Base64.encodeBase64String(UUID.randomUUID().toString().replaceAll("-","").getBytes());
        return str;
    }

}
相关推荐
wechatbot8884 小时前
企业微信语音通话实战:基于极客互动 API 打通语音拨通、接收与挂断全链路
微信·企业微信·企业微信scrm·企业微信api·企业微信sdk·企微ipad协议
极客互动API4 小时前
企业微信 API 二次开发:外部群机器人能力边界与实战指南
机器人·企业微信·企业微信二次开发·企业微信api·企业微信sdk·企业微信ipad协议
wechatbot8882 天前
企业微信外部群聊自动回复机器人开发实战
微信·机器人·企业微信·企业微信scrm·企业微信二次开发·企业微信api
极客互动API2 天前
企业微信外部联系人消息群发控制台效果展示
微信·企业微信·企微scrm·企业微信api·企微外部联系人
飞奔的瓜牛3 天前
Zabbix 部署与实战监控手册:Java 服务、端口、进程与邮件/企微告警
java·企业微信·zabbix
千舟软件11 天前
报修系统对接企业微信和钉钉的3种方式_千舟报修云
服务器·钉钉·企业微信
金融Tech趋势派16 天前
企业微信客户管理工具哪个好?按企业规模、行业与合规要求的三层判断标准
人工智能·企业微信
知之AI17 天前
桌面 RPA 入门:企业微信日常流程怎么拆、怎么选型(2026)
企业微信·rpa·选型·流程自动化·桌面自动化
超级新零售19 天前
服装订货会数字化:商品版本、订单状态与后续配发数据如何衔接?
大数据·小程序·企业微信
adrninistrat0r22 天前
通过企业微信消息向脚本传递短信验证码、Token 等信息
企业微信