2. Docking of public numbers with their own projects

Source: Internet
Author: User
Tags sha1 sha1 encryption

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

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.