The connection of the public number to its own project, that is, the public number will hand over the relevant events to your own project, you need to provide a portal, that is, a servlet URL
1. Create a servlet to process the validation message in the Doget method for the public number docking
1 Public classWeixinservletextendsHttpServlet {2 3 Private Static Final LongSerialversionuid = 1L;4 5 PublicWeixinservlet () {6 }7 8 Public voiddestroy () {9 }Ten One Public voiddoget (httpservletrequest request, httpservletresponse response) A throwsservletexception, IOException { - - //Encrypt Signature theString signature = request.getparameter ("signature"); - //time Stamp -String timestamp = request.getparameter ("timestamp"); - //Random number +String nonce = Request.getparameter ("nonce"); - //Random String +String echostr = Request.getparameter ("Echostr"); A atPrintWriter out =Response.getwriter (); - //Verify the request by checking the signature, if the verification is successful then return ECHOSTR, indicating the access is successful, otherwise the access fails - if(signutil.checksignature (signature, timestamp, nonce)) { - Out.print (ECHOSTR); - } - Out.close (); inout =NULL; - } to + Public voidDoPost (httpservletrequest request, httpservletresponse response) - throwsservletexception, IOException { the * } $ Panax Notoginseng Public voidInit ()throwsservletexception { - } the}
Source code for Signutil:
1 Public classSignutil {2 3 //consistent with token in the interface configuration information4 Private StaticString token = "Weixincourse";5 6 /**7 * Verify Signature8 * 9 * @paramSignatureTen * @paramtimestamp One * @paramnonce A * @return - */ - Public Static Booleanchecksignature (string signature, string timestamp, the String nonce) { -string[] arr =Newstring[] {token, timestamp, nonce}; - //three parameters of token, timestamp and nonce are sorted in dictionary order - Arrays.sort (arr); +StringBuilder content =NewStringBuilder (); - for(inti = 0; i < arr.length; i++) { + content.append (Arr[i]); A } atMessageDigest MD =NULL; -String tmpstr =NULL; - - Try { -MD = Messagedigest.getinstance ("SHA-1"); - //SHA1 encryption by stitching three parameter strings into a single string in byte[] Digest =md.digest (Content.tostring (). GetBytes ()); -TMPSTR =Bytetostr (digest); to}Catch(nosuchalgorithmexception e) { + e.printstacktrace (); - } the *Content =NULL; $ //The SHA1 encrypted string can be compared with signature, which identifies the request fromPanax Notoginseng returnTmpstr! =NULL? Tmpstr.equals (Signature.touppercase ()):false; - } the + Public Staticstring SHA1 (string content) { AMessageDigest MD =NULL; theString tmpstr =NULL; + - Try { $MD = Messagedigest.getinstance ("SHA-1"); $ //SHA1 encryption by stitching three parameter strings into a single string - byte[] Digest =md.digest (Content.tostring (). GetBytes ()); -TMPSTR =Bytetostr (digest); the}Catch(nosuchalgorithmexception e) { - e.printstacktrace ();Wuyi } the returntmpstr; - } Wu - /** About * Converts a byte array to a hexadecimal string $ * - * @paramByteArray - * @return - */ A Private StaticString Bytetostr (byte[] byteArray) { +String strdigest = ""; the for(inti = 0; i < bytearray.length; i++) { -Strdigest + =bytetohexstr (Bytearray[i]); $ } the returnstrdigest; the } the the /** - * Convert bytes to hexadecimal string in * the * @paramMByte the * @return About */ the Private StaticString Bytetohexstr (byteMByte) { the Char[] Digit = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', the' B ', ' C ', ' D ', ' E ', ' F ' }; + Char[] Temparr =New Char[2]; -Temparr[0] = digit[(MByte >>> 4) & 0X0F]; theTEMPARR[1] = digit[mbyte & 0X0F];Bayi theString s =NewString (Temparr); the returns; - } - the}
Second, deploy the project to the Sina Cloud and configure the servlet's request address to the public number.
2. Docking of public numbers with their own projects