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

}
相关推荐
数智化商业4 天前
企业微信服务商如何助力3C电器品牌增长 37%?数据与案例拆解
企业微信
小魏的马仔5 天前
【企业微信】接口报错:javax.net.ssl.SSLHandshakeException
网络协议·企业微信·ssl
胡萝卜的兔5 天前
ssh域名过期,消息推送到企业微信
运维·ssh·企业微信
微盛AI企微管家5 天前
企业微信AI功能升级:选对企业微信服务商协助四大AI场景落地
大数据·人工智能·企业微信
kevin_cat6 天前
微信群机器人-备份文件发送通知
git·bash·企业微信
key_3_feng7 天前
MCP驱动企业微信智能中枢:企业级机器人服务构建全攻略
企业微信·mcp
javachen__8 天前
Spring Boot配置error日志发送至企业微信
spring boot·后端·企业微信
IT小农工8 天前
通过API接口管理企业微信通讯录案例
企业微信
东哥说-MES|从入门到精通8 天前
企业微信智能表格高效使用指南
数据分析·项目管理·企业微信·智能表格
泰迪智能科技9 天前
案例分享|企微智能会话风控系统:为尚丰盈铝业筑牢沟通安全防线
安全·企业微信