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

}
相关推荐
天空属于哈夫克32 天前
打造私域闭环:CRM 如何驱动企微外部客户触达
自动化·企业微信·api
梦想的旅途22 天前
企业微信外部群自动化:一期交付应聚焦双向会话闭环
java·开发语言·机器人·自动化·maven·企业微信
天空属于哈夫克32 天前
医疗私域与电商社群:企微自动化落地的行业差异
自动化·企业微信
挨踢诗人2 天前
企业微信报销审批 × 金蝶云星空 费用凭证集成解决方案
企业微信
梦想的旅途25 天前
企业微信外部群消息自动推送实战
机器人·自动化·企业微信
2501_941982055 天前
Webhook 驱动:企业微信消息接收与自动回复
网络·机器人·自动化·企业微信
Kimgoeunlaogong6 天前
Clawdbot汉化版从零开始:Clawdbot前端控制台二次开发+UI主题定制
企业微信·前端开发·ai助手·clawdbot
金融Tech趋势派7 天前
企业微信私域实现高效增长的3步策略:精准获客+粘性留存+高效转化
大数据·人工智能·企业微信
河北小博博7 天前
OpenClaw 接入飞书 / 钉钉 / 企业微信:从 HTTP Webhook 到 WebSocket 长连接
钉钉·飞书·企业微信
金融Tech趋势派7 天前
企业微信SCRM哪个好?2026年企业微信客户管理工具服务商选型测评与金融汽车零售等行业实战指导
金融·汽车·企业微信