Java微信公眾號(hào)安全模式消息解密
本文實(shí)例為大家分享了Java微信公眾號(hào)安全模式消息解密的具體代碼,供大家參考,具體內(nèi)容如下
1.微信公眾平臺(tái)下載解密工具,導(dǎo)入項(xiàng)目中,根據(jù)demo解密消息,解密工具官方下載地址:點(diǎn)擊打開鏈接
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實(shí)體類
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兩個(gè)文件拷貝到 jdk\jre\lib\security目錄下
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring的BeanUtils.copyProperties屬性復(fù)制避坑指南
這篇文章主要介紹了Spring的BeanUtils.copyProperties屬性復(fù)制避坑指南,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08java實(shí)現(xiàn)多線程文件的斷點(diǎn)續(xù)傳
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)多線程文件的斷點(diǎn)續(xù)傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06SpringMVC 實(shí)現(xiàn)用戶登錄實(shí)例代碼
這篇文章主要介紹了SpringMVC 實(shí)現(xiàn)用戶登錄實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02Spring中XmlWebApplicationContext的實(shí)現(xiàn)
XmlWebApplicationContext是Spring?Framework中的一個(gè)重要類,本文主要介紹了Spring中XmlWebApplicationContext,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08JavaCV實(shí)現(xiàn)讀取視頻信息及自動(dòng)截取封面圖詳解
javacv可以幫助我們?cè)趈ava中很方便的使用OpenCV以及FFmpeg相關(guān)的功能接口。本文將利用Javacv實(shí)現(xiàn)在視頻網(wǎng)站中常見的讀取視頻信息和自動(dòng)獲取封面圖的功能,感興趣的可以了解一下2022-06-06springboot 多數(shù)據(jù)源的實(shí)現(xiàn)(最簡單的整合方式)
這篇文章主要介紹了springboot 多數(shù)據(jù)源的實(shí)現(xiàn)(最簡單的整合方式),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11