Request verification for secondary development WeChat

Source: Internet
Author: User
This article describes in detail the first article of Java secondary development. The Java request verification function has some reference value, interested friends can refer to this article for details about the first article of Java secondary development. The Java request verification function has some reference value, for more information, see

Prepare to use Java for a secondary development project. write the process here.

Article 1: perform request verification

Import Warehouse to: servlet-api.jar

Step 1:Create com. wtz. service and LoginServlet. java

Package com. wtz. service; import java. io. IOException; import java. io. printWriter; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import com. wtz. util. validationUtil;/*** @ author wangtianze QQ: 864620012 * @ date 8:11:32 on January 1, April 17, 2017 *

Version: 1.0

*

Description: request verification class

*/Public class LoginServlet extends HttpServlet {@ Override protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System. out. println ("get request ...... "); // 1. string signature = request. getParameter ("signature"); // 2. obtain the timestamp String timestamp = request. getParameter ("timestamp"); // 3. obtain the random number String nonce = request. getParameter ("nonce"); // 4. obtain the random String echostr = request. getParameter ("echostr"); System. out. println ("The encrypted string for obtaining the signature:" + signature); System. out. println ("get timestamp information:" + timestamp); System. out. println ("random number obtained:" + nonce); System. out. println ("get random string:" + echostr); PrintWriter out = response. getWriter (); // if the content of the echostr parameter is returned after the request is confirmed successfully, the access takes effect and becomes a developer. Otherwise, if (ValidationUtil. checkSignature (signature, timestamp, nonce) {out. print (echostr);} out. close (); out = null ;}}

Step 2:Create a package com. wtz. util and a new class Validation. java

Package com. wtz. util; import java. security. messageDigest; import java. security. noSuchAlgorithmException; import java. util. arrays;/*** @ author wangtianze QQ: 864620012 * @ date 8:35:57 on January 1, April 17, 2017 *

Version: 1.0

*

Description: request verification tool

*/Public class ValidationUtil {private static String token = "wangtianze"; public static boolean checkSignature (String signature, String timestamp, String nonce) {// 1. sort the token, timestamp, and nonce parameters. String [] str = new String [] {token, timestamp, nonce}; Arrays. sort (str); // 2. concatenates the three parameter strings into a string StringBuilder buff = new StringBuilder (); for (int I = 0; I >>4) & 0X0F]; temp [1] = digit [mByte & 0X0F]; String str = new String (temp); return str ;}}

This was done on the first day, and the request verification function was completed.

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.