需要傳4個參數:timestamp、nonce、msg_signature 進行解密獲取?ComponentVerifyTicket
但是微信文檔沒有寫明白的是 post過來的 xml 里面 只有 ?appid 和 加密后的字符串encrypt 其他 參數需要另行獲取
用request.getParameter() 或者 直接使用@RequestParam 注解獲取
<xml><AppId></Appid><Encrypt></Encrypt></xml>
然后就進行下一步的解密了
我用的官方的例子。。。但是很奇怪的問題是
XMLParse 這個類里面的方法 是需要Encrypt 和ToUserName的
然后我就把WXBizMsgCrypt.decryptMsg()方法改成以下代碼:
public String decryptMsg(String msgSignature, String timeStamp, String nonce, String encrypt)
throws AesException {
// 密鑰,公眾賬號的app secret
// 提取密文
// Object[] encrypt = XMLParse.extract(postData);
// 驗證安全簽名
String signature = SHA1.getSHA1(token, timeStamp, nonce, encrypt);
// 和URL中的簽名比較是否相等
// System.out.println("第三方收到URL中的簽名:" + msg_sign);
// System.out.println("第三方校驗簽名:" + signature);
if (!signature.equals(msgSignature)) {
throw new AesException(AesException.ValidateSignatureError);
}
// 解密
String result = decrypt(encrypt);
return result;
}
最后調用這個方法
@ResponseBody@RequestMapping(value = "/authorization/callBack", method ={RequestMethod.POST,RequestMethod.GET})@ApiOperation(value = "接收授權事件")public Result authorizationCallBack(HttpServletRequest request,HttpServletResponse response,
@RequestBody String xml,
@RequestParam(value = "signature", required = false) String signature,
@RequestParam(value = "timestamp", required = false) String timeStamp,
@RequestParam(value = "nonce", required = false) String nonce,
@RequestParam(value = "encrypt_type", required = false) String encryptType,
@RequestParam(value = "msg_signature", required = false) String msgSignature
) throws Exception {
logger.info("authorizationXml = "+xml);
logger.info("signature = "+signature +" timeStamp ="+timeStamp +" nonce = "+nonce+" msgSignature ="+msgSignature);
//TODO 未完成
//token, encodingAesKey, appId 這三個參數都是可以直接登錄微信開放平臺可以直接獲取到的
String encodingAesKey = "自己填";
String token = "自己填";
String appId = "自己填";
WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
//這里WxAuthorizationCallBackEvent 的是自己寫的xml類 獲取?Encrypt的值
WxAuthorizationCallBackEvent bean = JaxbUtil.converyToJavaBean(xml, WxAuthorizationCallBackEvent.class);
String decryptMsg = pc.decryptMsg(msgSignature, timeStamp, nonce, bean.getEncrypt());
logger.info("【success】 =" + decryptMsg);
response.getWriter().write("success");
? ? response.getWriter().flush();
? ? response.getWriter().close();
return null;
}
打印出來的 decryptMsg 等于
<xml>
<AppId>值</AppId>
<CreateTime>值</CreateTime>
<InfoType>component_verify_ticket<InfoType>
<ComponentVerifyTicket>一大串東西<ComponentVerifyTicket>
</xml>
然后就大功告成咯! ?
不知道是不是我的用法有問題還是。。。這個例子已經過時了(⊙o⊙)…?