How to enable the development mode of java WeChat Enterprise Account

Source: Internet
Author: User
This article describes how to enable the development mode of java enterprise code, interested partners can refer to this article for details about how to enable the development mode of java enterprise code. interested partners can refer

First, the enterprise ID development model is divided:Editing mode (normal mode)AndDevelopment Mode (callback mode)In the editing mode, you can only make simple custom menus and automatically reply to messages. to implement other functions, you must enable the developer mode.

I. message processing process in the editing and development modes

1. in editing mode, all business flows are configured on the server for processing.

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:

/*** Example code for encryption and decryption of messages sent from the public platform to the public account. ** @ copyright Copyright (c) 1998-2014 Tencent Inc. * // ----------------------------------------------------------------------------/*** for org. apache. commons. codec. binary. base64, * need to import shelf package commons-codec-1.9 (or other versions such as commons-codec-1.8) * Official: 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 the encryption and decryption interface (UTF-8 encoded string) for receiving and pushing messages to the public platform ). *
 
 
    *
  1. Third-Party reply to the public platform for encrypted messages
  2. *
  3. The third party receives the message sent by the public platform, verifies the security of the message, and decrypts the message.
  4. *
* Note: exception java. security. InvalidKeyException: illegal Key Size solution *
    *
  1. Download the JCE unrestricted permission policy file on the official website (JDK7: * http://www.php.cn/;/li> *
  2. After the download is decompressed, you can see local_policy.jarand us_export_policy.jarto and readme.txt
  3. *
  4. If JRE is installed, put the two jar files in the % JRE_HOME % \ lib \ security directory to overwrite the original files.
  5. *
  6. If JDK is installed, put the two jar files in the % JDK_HOME % \ jre \ lib \ security directory to overwrite the original file.
  7. *
*/Public class WXBizMsgCrypt {static Charset CHARSET = Charset. forName ("UTF-8"); Base64 base64 = new Base64 (); byte [] aesKey; String token; String corpId;/*** constructor * @ param token on the public platform, on the developer's token * @ param encodingAesKey public platform, the developer's EncodingAESKey * @ param corpId enterprise corpid ** @ throws AesException fails to be executed, please check the error code and specific error information for this exception */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 + "=") ;}/ *** decrypts the ciphertext. ** @ param text the ciphertext to be decrypted * @ return the plaintext decrypted * @ throws AesException aes decryption failure */String decrypt (String text) throws AesException {byte [] original; try {// Set the decryption mode to AES's CBC mode Cipher cipher = Cipher. getInstance ("AES/CBC/NoPadding"); Se CretKeySpec key_spec = new SecretKeySpec (aesKey, "AES"); IvParameterSpec iv = new IvParameterSpec (Arrays. copyOfRange (aesKey, 0, 16); cipher. init (Cipher. DECRYPT_MODE, key_spec, iv); // uses BASE64 to decode the ciphertext. byte [] encrypted = Base64.decodeBase64 (text); // decrypt original = cipher. doFinal (encrypted);} catch (Exception e) {e. printStackTrace (); throw new AesException (AesException. decryptAESError);} String x MlContent, from_corpid; try {// remove the character byte [] bytes = PKCS7Encoder. decode (original); // separate 16-bit random string, 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 (Arrays. copyOfRange (bytes, 20 + xmlLength, bytes. length), CHARSET );} Catch (Exception e) {e. printStackTrace (); throw new AesException (AesException. IllegalBuffer);} // if (! From_corpid.equals (corpId) {throw new AesException (AesException. validateCorpidError);} return xmlContent;}/*** verify the URL * @ param msgSignature signature string, corresponding to the msg_signature * @ param timeStamp of the URL parameter, corresponding to the timestamp * @ param nonce random string of the URL parameter, corresponding to the nonce * @ param echoStr random string of the URL parameter, the echostr ** @ return decrypted echostr * @ throws AesException of the URL parameter fails to be executed. check the exception error code and 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 ;}/ *** example code for encryption and decryption of messages sent to the public account by the public platform. ** @ copyright Copyright (c) 1998-2014 Tencent Inc. * // ---------------------------------------------------------------------------- package com. qq. weixin. mp. aes; import java. security. messageDigest; import java. util. arrays;/***** SHA1 class ** message signature interface for the public platform. */class SHA1 {/*** generate a secure signature using the SHA1 algorithm * @ param token ticket * @ param timestamp * @ param nonce random string * @ param encrypt ciphertext * @ return secure 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 sorting Arrays. sort (array); for (int I = 0; I <4; I ++) {sb. append (array [I]);} String str = sb. toString (); // Generate MessageDigest md = MessageDigest using the SHA1 signature. 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; /*** delete the plaintext completion character after decryption ** @ param decrypted the decrypted plaintext * @ return delete the plaintext after the completion character */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 );}}

III. Summary
The enterprise uses the msg_signature parameter to verify the request. If you confirm that the GET request comes from the enterprise number, the enterprise application decrypts the echostr parameter and returns the echostr plain text (no quotation marks) as is, the access verification takes effect, the callback mode can be enabled. Some functions will be implemented one after another. please wait!

The above is a detailed description of the steps for enabling the development mode of java enterprise code. For more information, see other related articles on php Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.