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

}
相关推荐
wjc12313132 天前
蓝印RPA|企业微信机器人Agent配置说明
机器人·企业微信·rpa
基鑫阁2 天前
Clawdbot机器学习部署:TensorFlow模型服务化
tensorflow·企业微信·图像识别·clawdbot
天空属于哈夫克32 天前
3分钟快速接入!实现企业微信外部群主动调用能力
自动化·企业微信·api·rpa
tianxiaxue12 天前
企业微信飞单私单行为监控与敏感行为监控
企业微信
tianxiaxue12 天前
企业微信聊天敏感词监控、提醒与拦截
企业微信
a752066283 天前
OpenClaw企业微信渠道配置教程|API模式+长连接+全部授权
人工智能·机器人·企业微信·openclaw部署·小龙虾一键安装
2501_941982053 天前
通过 API 实时监听企业微信外部群变更事件并同步本地数据库
android·自动化·企业微信·rpa
qingfeng154154 天前
企业微信机器人开发:如何实现自动化与智能运营?
人工智能·python·机器人·自动化·企业微信
qingfeng154154 天前
企业微信 API 自动化开发指南:从消息回调到智能运营实战
java·开发语言·python·自动化·企业微信
qingfeng154154 天前
企业微信消息监听实战:如何实时接收客户消息回调?
人工智能·python·自动化·企业微信