


Detailed explanation of the steps to start the development mode of Java WeChat enterprise account development
Mar 15, 2017 pm 05:49 PMThis article mainly introduces in detail how to open the development mode of java WeChat enterprise account development. Interested friends can refer to it
First of all, the development mode of WeChat enterprise account is divided into: Edit mode (normal mode) and development mode (callback mode), in edit mode, you can only do simple custom menu and automatic reply messages, if you want to realize other functions, you must turn on the developer mode.
1. Message processing process in edit mode and development mode
1. In edit mode, all business processes are configured on the WeChat server and processed by it
2. Development mode, the message is processed through a third-party server, and finally the message is sent to the user through the WeChat server
Development mode can handle more messages than editing mode, so you must turn on development mode first to develop more functions.
2. Turning on development mode
In callback mode, enterprises can not only actively call the enterprise numberinterface, but also receive messages from users or event. The received information uses XML data format, UTF8 encoding, and is encrypted in AES.
1. After turning on callback mode, configure the parameters as follows:
where url is the servlet to be accessed, token and EncodingAESKey are obtained randomly, but they must be consistent with those in the project.
2. Verify the validity of the URL
When you submit the above information, the enterprise account will send a GET request to the filled-in URL. The GET request carries four parameters. The enterprise is in You need to do urldecode processing when getting it, otherwise the verification will not be successful.
3. Code
CoreServlet1 class
public class CoreServlet1 extends HttpServlet { private static final long serialVersionUID = 4440739483644821986L; String sToken = "weixinCourse"; String sCorpID = "wxe510946434680dab"; String sEncodingAESKey = "DjlyZxgKiWRESIW2VnV9dSr7HsS7usWDfnwA8Q1ove1"; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { WXBizMsgCrypt wxcpt; try { wxcpt = new WXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID); String sVerifyMsgSig = request.getParameter("msg_signature"); String sVerifyTimeStamp = request.getParameter("timestamp"); String sVerifyNonce = request.getParameter("nonce"); String sVerifyEchoStr = request.getParameter("echostr"); String sEchoStr; sEchoStr = wxcpt.VerifyURL(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr); System.out.println("verifyurl echostr: " + sEchoStr); PrintWriter out = response.getWriter(); out.print(sEchoStr); out.close(); out = null; } catch (AesException e1) { e1.printStackTrace(); } } }
Tool class :
/** * 對公眾平臺發(fā)送給公眾賬號的消息加解密示例代碼. * * @copyright Copyright (c) 1998-2014 Tencent Inc. */ // ------------------------------------------------------------------------ /** * 針對org.apache.commons.codec.binary.Base64, * 需要導(dǎo)入架包c(diǎn)ommons-codec-1.9(或commons-codec-1.8等其他版本) * 官方下載地址:http://ipnx.cn/ */ package com.qq.weixin.mp.aes; import java.nio.charset.Charset; import java.util.Arrays; import java.util.Random; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; /** * 提供接收和推送給公眾平臺消息的加解密接口(UTF8編碼的字符串). * <ol> * <li>第三方回復(fù)加密消息給公眾平臺</li> * <li>第三方收到公眾平臺發(fā)送的消息,驗(yàn)證消息的安全性,并對消息進(jìn)行解密。</li> * </ol> * 說明:異常java.security.InvalidKeyException:illegal Key Size的解決方案 * <ol> * <li>在官方網(wǎng)站下載JCE無限制權(quán)限策略文件(JDK7的下載地址: * http://ipnx.cn/;/li> * <li>下載后解壓,可以看到local_policy.jar和US_export_policy.jar以及readme.txt</li> * <li>如果安裝了JRE,將兩個jar文件放到%JRE_HOME%\lib\security目錄下覆蓋原來的文件</li> * <li>如果安裝了JDK,將兩個jar文件放到%JDK_HOME%\jre\lib\security目錄下覆蓋原來文件</li> * </ol> */ public class WXBizMsgCrypt { static Charset CHARSET = Charset.forName("utf-8"); Base64 base64 = new Base64(); byte[] aesKey; String token; String corpId; /** * 構(gòu)造函數(shù) * @param token 公眾平臺上,開發(fā)者設(shè)置的token * @param encodingAesKey 公眾平臺上,開發(fā)者設(shè)置的EncodingAESKey * @param corpId 企業(yè)的corpid * * @throws AesException 執(zhí)行失敗,請查看該異常的錯誤碼和具體的錯誤信息 */ public WXBizMsgCrypt(String token, String encodingAesKey, String corpId) throws AesException { if (encodingAesKey.length() != 43) { throw new AesException(AesException.IllegalAesKey); } this.token = token; this.corpId = corpId; aesKey = Base64.decodeBase64(encodingAesKey + "="); } /** * 對密文進(jìn)行解密. * * @param text 需要解密的密文 * @return 解密得到的明文 * @throws AesException aes解密失敗 */ String decrypt(String text) throws AesException { byte[] original; try { // 設(shè)置解密模式為AES的CBC模式 Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); SecretKeySpec key_spec = new SecretKeySpec(aesKey, "AES"); IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey, 0, 16)); cipher.init(Cipher.DECRYPT_MODE, key_spec, iv); // 使用BASE64對密文進(jìn)行解碼 byte[] encrypted = Base64.decodeBase64(text); // 解密 original = cipher.doFinal(encrypted); } catch (Exception e) { e.printStackTrace(); throw new AesException(AesException.DecryptAESError); } String xmlContent, from_corpid; try { // 去除補(bǔ)位字符 byte[] bytes = PKCS7Encoder.decode(original); // 分離16位隨機(jī)字符串,網(wǎng)絡(luò)字節(jié)序和corpId byte[] networkOrder = Arrays.copyOfRange(bytes, 16, 20); int xmlLength = recoverNetworkBytesOrder(networkOrder); xmlContent = new String(Arrays.copyOfRange(bytes, 20, 20 + xmlLength), CHARSET); from_corpid = new String(Arrays.copyOfRange(bytes, 20 + xmlLength, bytes.length), CHARSET); } catch (Exception e) { e.printStackTrace(); throw new AesException(AesException.IllegalBuffer); } // corpid不相同的情況 if (!from_corpid.equals(corpId)) { throw new AesException(AesException.ValidateCorpidError); } return xmlContent; } /** * 驗(yàn)證URL * @param msgSignature 簽名串,對應(yīng)URL參數(shù)的msg_signature * @param timeStamp 時間戳,對應(yīng)URL參數(shù)的timestamp * @param nonce 隨機(jī)串,對應(yīng)URL參數(shù)的nonce * @param echoStr 隨機(jī)串,對應(yīng)URL參數(shù)的echostr * * @return 解密之后的echostr * @throws AesException 執(zhí)行失敗,請查看該異常的錯誤碼和具體的錯誤信息 */ public String VerifyURL(String msgSignature, String timeStamp, String nonce, String echoStr) throws AesException { String signature = SHA1.getSHA1(token, timeStamp, nonce, echoStr); if (!signature.equals(msgSignature)) { throw new AesException(AesException.ValidateSignatureError); } String result = decrypt(echoStr); return result; } } /** * 對公眾平臺發(fā)送給公眾賬號的消息加解密示例代碼. * * @copyright Copyright (c) 1998-2014 Tencent Inc. */ // ------------------------------------------------------------------------ package com.qq.weixin.mp.aes; import java.security.MessageDigest; import java.util.Arrays; /** * SHA1 class * * 計算公眾平臺的消息簽名接口. */ class SHA1 { /** * 用SHA1算法生成安全簽名 * @param token 票據(jù) * @param timestamp 時間戳 * @param nonce 隨機(jī)字符串 * @param encrypt 密文 * @return 安全簽名 * @throws AesException */ public static String getSHA1(String token, String timestamp, String nonce, String encrypt) throws AesException { try { String[] array = new String[] { token, timestamp, nonce, encrypt }; StringBuffer sb = new StringBuffer(); // 字符串排序 Arrays.sort(array); for (int i = 0; i < 4; i++) { sb.append(array[i]); } String str = sb.toString(); // SHA1簽名生成 MessageDigest md = MessageDigest.getInstance("SHA-1"); md.update(str.getBytes()); byte[] digest = md.digest(); StringBuffer hexstr = new StringBuffer(); String shaHex = ""; for (int i = 0; i < digest.length; i++) { shaHex = Integer.toHexString(digest[i] & 0xFF); if (shaHex.length() < 2) { hexstr.append(0); } hexstr.append(shaHex); } return hexstr.toString(); } catch (Exception e) { e.printStackTrace(); throw new AesException(AesException.ComputeSignatureError); } } } class PKCS7Encoder { static Charset CHARSET = Charset.forName("utf-8"); static int BLOCK_SIZE = 32; /** * 刪除解密后明文的補(bǔ)位字符 * * @param decrypted 解密后的明文 * @return 刪除補(bǔ)位字符后的明文 */ static byte[] decode(byte[] decrypted) { int pad = (int) decrypted[decrypted.length - 1]; if (pad < 1 || pad > 32) { pad = 0; } return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad); } }
3. Summary
The enterprise verifies the request through the parameter msg_signature. If the GET is confirmed If the request comes from an enterprise account, then the enterprise application decrypts the echostr parameter and returns the echostr plaintext as it is (no quotes), then the access verification takes effect and the callback mode can be turned on. After it is turned on, some functions will be implemented one after another, so stay tuned!
The above is the detailed content of Detailed explanation of the steps to start the development mode of Java WeChat enterprise account development. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)