eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/temp/service/impl/WechatRobotServiceImpl.java
@@ -3,6 +3,7 @@ import com.alibaba.fastjson.JSONObject; import lombok.RequiredArgsConstructor; import me.zhengjie.modules.temp.service.WechatRobotService; import me.zhengjie.utils.weixin.AesException; import me.zhengjie.utils.weixin.WXBizJsonMsgCrypt; import org.springframework.stereotype.Service; @@ -42,8 +43,9 @@ @Override public Object handleRobotCallback(String msgSignature, String timestamp, String nonce, HttpServletRequest request, HttpServletResponse response) { WXBizJsonMsgCrypt wxcpt = null; try { WXBizJsonMsgCrypt wxcpt = new WXBizJsonMsgCrypt(TOKEN, ENCODING_AES_KEY, CORP_ID); wxcpt = new WXBizJsonMsgCrypt(TOKEN, ENCODING_AES_KEY, CORP_ID); // 读取回调数据 StringBuilder sb = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream())); @@ -55,13 +57,20 @@ String callbackData = sb.toString(); JSONObject jsonObject = JSONObject.parseObject(callbackData); String decryptedMessage = wxcpt.DecryptMsg(msgSignature, timestamp, nonce, jsonObject.toString()); String decryptedMessage = wxcpt.DecryptRobotMsg(msgSignature, timestamp, nonce, jsonObject.toString()); System.out.println("接收到机器人发送的加密消息:" + jsonObject.toJSONString()); System.out.println("接收到机器人发送的解密后消息:" + decryptedMessage); return wxcpt.EncryptMsg("消息已收到", "", nonce); } catch (Exception e) { e.printStackTrace(); try { if (wxcpt != null) { return wxcpt.EncryptMsg("消息接收出现异常,请联系管理员处理 o(╥﹏╥o...)", "", nonce); } } catch (AesException aesException) { aesException.printStackTrace(); } } return null; eladmin-master/eladmin-system/src/main/java/me/zhengjie/utils/weixin/JsonParse.java
@@ -62,4 +62,35 @@ return String.format(format, encrypt, signature, timestamp, nonce); } /** * 提取出 JSON 包中的加密消息(智能机器人) * @param jsontext 待提取的json字符串 * @return 提取出的加密消息字符串 * @throws AesException */ public static Object[] extractRobot(String jsontext) throws AesException { Object[] result = new Object[3]; try { JSONObject json = new JSONObject(jsontext); String encrypt_msg = json.getString("encrypt"); // System.out.println(json.toString()); // String msgid = json.getString("msgid"); // String aibotid = json.getString("aibotid"); // String chatid = json.getString("chatid"); // String chattype = json.getString("chattype"); // JSONObject from = json.getJSONObject("from"); // String userid = from.getString("userid"); // String msgtype = json.getString("msgtype"); // JSONObject text = json.getJSONObject("text"); // String content = text.getString("content"); result[0] = encrypt_msg; return result; } catch (Exception e) { e.printStackTrace(); throw new AesException(AesException.ParseJsonError); } } } eladmin-master/eladmin-system/src/main/java/me/zhengjie/utils/weixin/WXBizJsonMsgCrypt.java
@@ -286,4 +286,42 @@ return result; } /** * 检验智能机器人消息的真实性,并且获取解密后的明文. * <ol> * <li>利用收到的密文生成安全签名,进行签名验证</li> * <li>若验证通过,则提取json中的加密消息</li> * <li>对消息进行解密</li> * </ol> * * @param msgSignature 签名串,对应URL参数的msg_signature * @param timeStamp 时间戳,对应URL参数的timestamp * @param nonce 随机串,对应URL参数的nonce * @param postData 密文,对应POST请求的数据 * * @return 解密后的原文 * @throws AesException 执行失败,请查看该异常的错误码和具体的错误信息 */ public String DecryptRobotMsg(String msgSignature, String timeStamp, String nonce, String postData) throws AesException { // 密钥,公众账号的app secret // 提取密文 Object[] encrypt = JsonParse.extractRobot(postData); // 验证安全签名 String signature = SHA1.getSHA1(token, timeStamp, nonce, encrypt[0].toString()); // 和URL中的签名比较是否相等 // System.out.println("第三方收到URL中的签名:" + msg_sign); // System.out.println("第三方校验签名:" + signature); if (!signature.equals(msgSignature)) { throw new AesException(AesException.ValidateSignatureError); } // 解密 String result = decrypt(encrypt[0].toString()); return result; } }