企业微信中设置回调接口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;
    }

}
相关推荐
无心水2 天前
【Hermes:多平台接入】19、钉钉/飞书/企业微信:国内办公场景接入指南 —— 将 Honcho 智能体部署到你的工作聊天软件
人工智能·钉钉·飞书·企业微信·openclaw·hermes·honcho
天空属于哈夫克32 天前
企业微信API常见的错误和解决方案
java·数据库·企业微信
wzl202612133 天前
企微私域新客 AI 运营实战:轻量化工具落地指南
大数据·人工智能·企业微信
tianxiaxue13 天前
企微永久有效无限建群的群活码
企业微信
wzl202612133 天前
企微 5.0.8 “记录面聊” 深度解读:AI 打通线下私域最后一公里
人工智能·企业微信
byoass4 天前
企业云盘权限管理深度对比:巴别鸟、联想Filez、腾讯企微云盘
网络·安全·云计算·企业微信
tianxiaxue14 天前
企业微信超时未回复如何进行语音提醒?
企业微信
小白跃升坊5 天前
OpenClaw 集成企业微信:功能应用解析大全
企业微信
tianxiaxue15 天前
企微群内超时提醒通知到飞书/钉钉?
钉钉·飞书·企业微信
小白跃升坊5 天前
OpenClaw集成企业微信:功能应用解析大全
企业微信·ai agent·智能体·ai办公·openclaw