钉钉回调加解密类库和对应demo
- 实例化加解密类,入参是token, aesKey, ownerKey(企业回调是corpId, 三方应用回调是suiteKey) DingCallbackCrypto callbackCrypto = new DingCallbackCrypto(TOKEN, AES_KEY, OWNER_KEY);
- 解密钉钉推送的数据,从http请求中获取解密参数 String decryptMsg = callbackCrypto.getDecryptMsg(msg_signature, timeStamp, nonce, encrypt);
- 返回success的加密字符串 Map<String, String> successMap = callbackCrypto.getEncryptedMap("success");
// 1. 从http请求中获取加解密参数
String msg_signature = request.getParameter("msg_signature");
if (msg_signature == null) {
msg_signature = request.getParameter("signature");
}
String timeStamp = request.getParameter("timeStamp");
if (timeStamp == null) {
timeStamp = request.getParameter("timestamp");
}
String nonce = request.getParameter("nonce");
String encrypt = bodyJson.getString("encrypt");
// 2. 使用加解密类型
DingCallbackCrypto callbackCrypto = new DingCallbackCrypto(TOKEN, AES_KEY, OWNER_KEY);
final String decryptMsg = callbackCrypto.getDecryptMsg(msg_signature, timeStamp, nonce, encrypt);
// 3. 反序列化回调事件json数据
JSONObject eventJson = JSON.parseObject(decryptMsg);
String eventType = eventJson.getString("EventType");
// 4. 根据EventType分类处理
if ("check_url".equals(eventType)) {
// 测试回调url的正确性
} else if ("user_add_org".equals(eventType)) {
// 处理通讯录用户增加时间
} else {
// 添加其他已注册的
}
// 5. 返回success的加密数据
Map<String, String> successMap = callbackCrypto.getEncryptedMap("success");
return successMap;
- JDK6,JDK7需要下载JCE无限制权限策略文件
- 依赖commons-codes包
- 依赖Crypto包进行AES的加解密
- 依赖Crypto包进行AES的加解密
- 依赖openssl_encrypt方法加解密,版本依赖 (PHP 5 >= 5.3.0, PHP 7)