First of all, the development model of micro-credit enterprise is divided into: edit mode (normal mode ) and development mode (callback mode) , in the editing mode, can only do simple custom menu and automatic reply message, in order to implement other functions have to open the developer mode.
One, edit mode and development mode of the message processing flow
1. In edit mode, all business processes are configured on the micro-trust server, which handles
2. Development mode, the message through the Third-party server processing, and finally through the micro-mail server to send the message to the user
Development mode can handle more messages than edit mode, so it is necessary to open development mode to develop more functions.
Second, the development of the mode of opening
In callback mode, the enterprise can not only actively invoke the enterprise number interface, but also receive the user's message or event. The received information is used in XML data format, UTF8 encoding, and is encrypted in AES mode .
1. After the callback mode is turned on, configure the parameters as follows:
Where the URL is to be accessed Servlet,token and Encodingaeskey are randomly acquired, but are consistent with the project.
2. Verify the validity of the URL
When you submit the above information, the enterprise will send the GET request to the completed URL, get request carries four parameters, the enterprise needs to do urldecode processing , otherwise it will verify unsuccessful.
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 {WXB
Izmsgcrypt 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 ();
}
}
}
Tools class:
/** * To decrypt the sample code for 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 of commons-codec-1.8) * Official Download Address: Http://commons.apache.org/proper/commons-codec
/DOWNLOAD_CODEC.CGI * * 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 a decryption interface (UTF8 encoded string) that receives and pushes messages to public platforms. * <ol> * <li> Third party reply encrypted message to public platform </li> * <li> third parties receive messages from the public platform, verify the security of the message, and decrypt the message. </li> * </ol> * Description: Exception java.security.InvalidKeyException:illegal Key Size solution * <ol> * <li> in the official network Station download JCE Unrestricted permission policy file (JDK7 download address: * http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.htmL</li> * <li> Uncompressed after downloading, you can see Local_policy.jar and Us_export_policy.jar as well as readme.txt</li> * <li> If JRE is installed, put two jar files into the%jre_home%\lib\security directory and overwrite the original file </li> * <li> If the JDK is installed, put two jar files into the%jdk_home%\jre\ Lib\security Directory Overwrite original file </li> * </ol>/public class Wxbizmsgcrypt {static Charset Charset = Charset.forname
("Utf-8");
Base64 base64 = new Base64 ();
Byte[] Aeskey;
String token;
String Corpid; /** * Constructor * @param token public platform, developer-Set token * @param encodingaeskey public platform, developer set Encodingaeskey * @param corpid Enterprise Corp. ID * * * @throws aesexception execution failed, check the error code for the exception and the specific error message/public Wxbizmsgcrypt (string token, string encodingaeskey, Stri Ng Corpid) throws Aesexception {if (Encodingaeskey.length ()!=) {throw new Aesexception (Aesexception.illegalaeskey
);
} This.token = token;
This.corpid = Corpid;
Aeskey = base64.decodebase64 (encodingaeskey + "=");
/** * To decrypt the ciphertext. * * @param text need to decrypt the ciphertext * @return decrypted plaintext * @throws aesexceptionAES decryption failure/String decrypt (string text) throws Aesexception {byte[] original;
try {//Set the CBC mode Cipher Cipher = cipher.getinstance ("aes/cbc/nopadding") for the decryption mode AES;
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);
Decryption original = cipher.dofinal (encrypted);
catch (Exception e) {e.printstacktrace ();
throw new Aesexception (Aesexception.decryptaeserror);
String xmlcontent, From_corpid;
try {//Remove complement character byte[] bytes = Pkcs7encoder.decode (original);
Separation of 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, xmllength), CHARSET); From_corpid = new String (Arrays.copyofrange (bytes, + xmllength, bytes.length), CHARSET);
catch (Exception e) {e.printstacktrace ();
throw new Aesexception (Aesexception.illegalbuffer);
}//Corpid not the same case if (!from_corpid.equals (corpid)) {throw new aesexception (Aesexception.validatecorpiderror);
return xmlcontent; /** * Verify URL * @param msgsignature signature string, corresponding URL parameter msg_signature * @param timeStamp timestamp, timeStamp * @param corresponding URL parameter non Ce random string, corresponding URL parameter nonce * @param echostr random string, corresponding URL parameter echostr * * * @return after decryption Echostr * @throws Aesexception failed to perform, see the exception Error code and specific error message */public string Verifyurl (string msgsignature, String TimeStamp, String nonce, String echostr) throws Ae
sexception {String signature = SHA1.GETSHA1 (token, TimeStamp, nonce, ECHOSTR);
if (!signature.equals (msgsignature)) {throw new aesexception (Aesexception.validatesignatureerror);
String result = Decrypt (ECHOSTR);
return result;
}/** * To decrypt the sample code for messages sent to public accounts 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 * Computes the message signature interface for the public platform. * * Class SHA1 {/** * SHA1 algorithm to generate secure signature * @param token bill * @param timestamp 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 sort arrays.sort (array);
for (int i = 0; i < 4; i++) {sb.append (array[i]);
String str = sb.tostring ();
SHA1 signature generation 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; /** * Remove the complement character of the decrypted plaintext * * @param decrypted clear text after decryption * @return Clear text after deletion of the complement character/static byte[] Decode (byte[) decrypted) {in
t pad = (int) decrypted[decrypted.length-1];
if (Pad < 1 | | | Pad >) {pad = 0;
Return Arrays.copyofrange (decrypted, 0, Decrypted.length-pad);
}
}
Third, summary
The enterprise verifies the request through the parameter msg_signature, if it confirms that the GET request comes from the enterprise number, then the enterprise application decrypts the ECHOSTR parameter and returns ECHOSTR plaintext (without quotation marks), then the access verification takes effect and the callback mode can be opened. After opening some functions will be implemented, please look forward to!
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.