In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how Java implements Wechat request verification function. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Need to import library: servlet-api.jar
Step 1: create a new package com.wtz.service and a new class 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 17 April 2017 8:11:32 *
Version:1.0
*
Description: Wechat 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. Obtain the encrypted string String signature = request.getParameter ("signature") of Wechat signature; / / 2. Get the timestamp information String timestamp = request.getParameter ("timestamp"); / / 3. Get the random number String nonce = request.getParameter ("nonce"); / / 4. Get the random string String echostr = request.getParameter ("echostr"); System.out.println ("get the encrypted string for Wechat signature:" + signature); System.out.println ("get timestamp information:" + timestamp); System.out.println ("get random number:" + nonce); System.out.println ("get random string:" + echostr); PrintWriter out = response.getWriter () / / if the verification request is confirmed to be successful and returns the echostr parameter content as is, the connection takes effect and the developer succeeds, otherwise if (ValidationUtil.checkSignature (signature, timestamp, nonce)) {out.print (echostr);} out.close (); out = null;}
Step 2: create a new 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 17 April 2017 8:35:57 *
Version:1.0
*
Description: Wechat request verification tool class
* / public class ValidationUtil {private static String token = "wangtianze"; public static boolean checkSignature (String signature,String timestamp,String nonce) {/ / 1. Sort the three parameters of token,timestamp,nonce String [] str = new String [] {token,timestamp,nonce}; Arrays.sort (str); / / 2. Concatenate the three parameter strings into a single string StringBuilder buff = new StringBuilder (); for (int item0 0X0F I > 4) & 0X0F]; temp [1] = digit [mByte & 0X0F]; String str = new String (temp); return str }} this is the end of the article on "how Java implements Wechat request verification function". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.