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)文章
SpringBoot+Mybatis+Vue 實(shí)現(xiàn)商品模塊的crud操作
這篇文章主要介紹了SpringBoot+Mybatis+Vue 實(shí)現(xiàn)商品模塊的crud操作,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
解決springcloud 配置gateway 出現(xiàn)錯(cuò)誤的問題
今天給大家分享springcloud 配置gateway 出現(xiàn)錯(cuò)誤的問題,其實(shí)解決方法很簡單,只需要降低springcloud版本,改成Hoxton.SR5就好了,再次改成Hoxton.SR12,也不報(bào)錯(cuò)了,下面給大家展示下,感興趣的朋友一起看看吧2021-11-11
RSA加密算法java簡單實(shí)現(xiàn)方法(必看)
下面小編就為大家?guī)硪黄猂SA加密算法java簡單實(shí)現(xiàn)方法(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
java計(jì)算給定字符串中出現(xiàn)次數(shù)最多的字母和該字母出現(xiàn)次數(shù)的方法
這篇文章主要介紹了java計(jì)算給定字符串中出現(xiàn)次數(shù)最多的字母和該字母出現(xiàn)次數(shù)的方法,涉及java字符串的遍歷、轉(zhuǎn)換及運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
淺析Java 數(shù)據(jù)結(jié)構(gòu)常用接口與類
本篇文章主要介紹了Java中的數(shù)據(jù)結(jié)構(gòu),Java工具包提供了強(qiáng)大的數(shù)據(jù)結(jié)構(gòu)。需要的朋友可以參考下2017-04-04
Java中JSON字符串進(jìn)行各種轉(zhuǎn)換的方法小結(jié)
Gson和Hutool的JSONUtil都是常用的用于處理JSON數(shù)據(jù)的工具庫,它們提供了簡單易用的API來進(jìn)行JSON字符串的解析、轉(zhuǎn)換和操作,下面就跟隨小編一起學(xué)習(xí)一下如果使用他們實(shí)現(xiàn)JSON字符串的各種轉(zhuǎn)換吧2024-01-01

