In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces java Wechat enterprise development mode how to open, the article introduces in great detail, has a certain reference value, interested friends must read!
First of all, the development mode of Wechat Enterprise is divided into editing mode (normal mode) and development mode (callback mode). In editing mode, you can only do simple custom menus and automatic reply messages. If you want to achieve other functions, you have to open the developer mode.
I. message processing flow in editing mode and development mode
1. In editing mode, all business processes are configured on the Wechat server, which handles
two。 In the development mode, the message is processed by a third-party server and finally sent to the user through the Wechat server.
The development mode can handle more messages than the editing mode, so you have to open the development mode before you can develop more functions.
Second, the opening of the development mode
In callback mode, the enterprise can not only actively call the enterprise number interface, but also receive messages or events from users. The received information is in XML data format, UTF8 encoding, and encrypted in AES mode.
1. When callback mode is enabled, you need to configure the following parameters:
Where the servlet,token and EncodingAESKey to be accessed by url are randomly obtained, but consistent with those in the project.
two。 Verify the validity of URL
When you submit the above information, the enterprise ID will send a GET request to the entered URL. The GET request carries four parameters. The enterprise needs to do urldecode processing when obtaining the information, 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"); 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:
/ * sample code for encrypting and decrypting messages sent to public accounts by the public platform. * * @ copyright Copyright (c) 1998-2014 Tencent Inc. * / /-- / * * for org.apache.commons.codec.binary.Base64 * need to import rack package commons-codec-1.9 (or other versions such as commons-codec-1.8) * official download address: http://www.php.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;/** * provides an encryption and decryption interface (UTF8-encoded string) to receive and push messages to the public platform. * * the third party replies to the encrypted message to the public platform * the third party receives the message sent by the public platform, verifies the security of the message, and decrypts the message. * * Note: solution to exception java.security.InvalidKeyException:illegal Key Size * * download the JCE unrestricted permission policy file from the official website (download address of JDK7: * http://www.php.cn/; / li > * after downloading and decompressing, you can see local_policy.jar and US_export_policy.jar as well as readme.txt * if JRE is installed, put two jar files in% JRE_HOME%\ lib\ security directory to overwrite the original file * if JDK is installed, put two jar files in% JDK_HOME%\ jre\ lib\ security directory to overwrite the original file * * / public class WXBizMsgCrypt {static Charset CHARSET = Charset.forName ("utf-8") Base64 base64 = new Base64 (); byte [] aesKey; String token; String corpId On the / * * constructor * @ param token public platform, on the token * @ param encodingAesKey public platform set by the developer, the corpid * * @ throws AesException execution of the EncodingAESKey * @ param corpId enterprise set by the developer failed. Please check the error code of the exception and the specific error message * / 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 + "="); decrypt the ciphertext. * * @ param text ciphertext to be decrypted * @ return decrypted plaintext * @ throws AesException aes decryption failed * / String decrypt (String text) throws AesException {byte [] original; try {/ / set CBC mode to AES 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); / / use BASE64 to decode ciphertext byte [] encrypted = Base64.decodeBase64 (text); / / decrypt original = cipher.doFinal (encrypted);} catch (Exception e) {e.printStackTrace (); throw new AesException (AesException.DecryptAESError);} String xmlContent, from_corpid; try {/ / remove complement characters byte [] bytes = PKCS7Encoder.decode (original) / / separate 16-bit random strings, network byte order and 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 (bytes, 20 + xmlLength, bytes.length), CHARSET);} catch (Exception e) {e.printStackTrace (); throw new AesException (AesException.IllegalBuffer) } / / if the corpid is different, if (! from_corpid.equals (corpId)) {throw new AesException (AesException.ValidateCorpidError);} return xmlContent } / * verify URL * @ param msgSignature signature string, corresponding to msg_signature * @ param timeStamp timestamp of URL parameter, timestamp * @ param nonce random string of URL parameter, nonce * @ param echoStr random string of URL parameter, echostr * @ throws AesException of decrypted echostr * * @ return of URL parameter failed Please check the error code of the exception and the specific error message * / 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;}} / * * sample code for encrypting and decrypting messages sent to public accounts on public platforms. * * @ copyright Copyright (c) 1998-2014 Tencent Inc. * / /-package com.qq.weixin.mp.aes;import java.security.MessageDigest;import java.util.Arrays / * * SHA1 class * * calculates the message signature interface of the public platform. * / class SHA1 {/ * generate security signature using SHA1 algorithm * @ param token ticket * @ param timestamp timestamp * @ param nonce random string * @ param encrypt ciphertext * @ return security signature * @ 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 () / / string sort 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;/** * 删除解密后明文的补位字符 * * @param decrypted 解密后的明文 * @return 删除补位字符后的明文 */ 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);}} these are all the contents of this article entitled "how to turn on the Development Mode of java Wechat Enterprise". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.