Java微信公眾號安全模式消息解密
本文實例為大家分享了Java微信公眾號安全模式消息解密的具體代碼,供大家參考,具體內(nèi)容如下
1.微信公眾平臺下載解密工具,導(dǎo)入項目中,根據(jù)demo解密消息,解密工具官方下載地址:點擊打開鏈接
public static String streamToString(HttpServletRequest request) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* xml轉(zhuǎn)為map集合
*
* @param request
* @param msg
* @return
* @throws IOException
* @throws DocumentException
*/
public static Map<String, String> xmlToMap(HttpServletRequest request, Message msg) throws Exception {
SAXReader reader = new SAXReader();
String token = "";
String encodingAesKey = "";
String appId = "";
//獲取加密消息xml字符串
/* String format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>";
Document document = reader.read(request.getInputStream());
Element rootElement = document.getRootElement();
Element encrypt = rootElement.element("Encrypt");*/
// String fromXML = String.format(format, encrypt.getText());
String fromXML = streamToString(request);
//解密消息
WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
//獲得解密消息
String result = pc.decryptMsg(msg.getMsg_signature(), msg.getTimestamp(), msg.getNonce(), fromXML);
Map<String, String> map = new HashMap<>(6);
//將解密后的消息轉(zhuǎn)為xml
Document doc = DocumentHelper.parseText(result);
Element root = doc.getRootElement();
List<Element> list = root.elements();
for (Element e : list) {
map.put(e.getName(), e.getText());
}
return map;
}
Message實體類
package com.caisin.weixin.domain;
import lombok.Data;
@Data
public class Message {
private String signature;
private String timestamp;
private String nonce;
private String openid;
private String msg_signature;
private String encrypt_type;
}
2.將JDK中 jdk\jre\lib\security\policy\unlimited目錄中l(wèi)ocal_policy.jar和US_export_policy.jar兩個文件拷貝到 jdk\jre\lib\security目錄下

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring的BeanUtils.copyProperties屬性復(fù)制避坑指南
這篇文章主要介紹了Spring的BeanUtils.copyProperties屬性復(fù)制避坑指南,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
Spring中XmlWebApplicationContext的實現(xiàn)
XmlWebApplicationContext是Spring?Framework中的一個重要類,本文主要介紹了Spring中XmlWebApplicationContext,具有一定的參考價值,感興趣的可以了解一下2024-08-08
JavaCV實現(xiàn)讀取視頻信息及自動截取封面圖詳解
javacv可以幫助我們在java中很方便的使用OpenCV以及FFmpeg相關(guān)的功能接口。本文將利用Javacv實現(xiàn)在視頻網(wǎng)站中常見的讀取視頻信息和自動獲取封面圖的功能,感興趣的可以了解一下2022-06-06
springboot 多數(shù)據(jù)源的實現(xiàn)(最簡單的整合方式)
這篇文章主要介紹了springboot 多數(shù)據(jù)源的實現(xiàn)(最簡單的整合方式),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

