Secondary development of WeChat request verification

Y2J
Release: 2017-05-10 09:22:46
Original
1843 people have browsed it

This article mainly introduces the first part of the secondary development of Java WeChat in detail. The Java WeChat request verification function has a certain reference value. Interested friends can refer to it

Prepare to use To do a WeChat secondary development project in Java, write the process here.

The first article, do WeChat request verification

Need to import the library: servlet-api.jar

The first step:New package com.wtz .service, create 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 2017年4月17日 下午8:11:32
 * <p>version:1.0</p>
 *  <p>description:微信请求验证类</p>
 */
public class LoginServlet extends HttpServlet {

 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  System.out.println("get请求。。。。。。");
  
  //1.获得微信签名的加密字符串
  String signature = request.getParameter("signature");
  
  //2.获得时间戳信息
  String timestamp = request.getParameter("timestamp");
   
  //3.获得随机数
  String nonce = request.getParameter("nonce");
  
  //4.获得随机字符串
  String echostr = request.getParameter("echostr");
  
  System.out.println("获得微信签名的加密字符串:"+signature);
  System.out.println("获得时间戳信息:"+timestamp);
  System.out.println("获得随机数:"+nonce);
  System.out.println("获得随机字符串:"+echostr);
  
  PrintWriter out = response.getWriter();
  
  //验证请求确认成功原样返回echostr参数内容,则接入生效,成为开发者成功,否则失败
  if(ValidationUtil.checkSignature(signature, timestamp, nonce)){
   out.print(echostr);
  }
  
  out.close();
  out = null;
 }
}
Copy after login

Step 2: Create a new package com.wtz.util, create 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 2017年4月17日 下午8:35:57
 * <p>version:1.0</p>
 *  <p>description:微信请求校验工具类</p>
 */
public class ValidationUtil {
 private static String token = "wangtianze";
 
 public static boolean checkSignature(String signature,String timestamp,String nonce){
  //1.将token,timestamp,nonce三个参数进行排序
  String[] str = new String[]{token,timestamp,nonce};
  Arrays.sort(str);
  
  //2.将三个参数字符串拼接成一个字符串
  StringBuilder buff = new StringBuilder();
  for(int i=0;i<buff.length();i++){
   buff.append(str[i]);
  }
  
  //3.进行sha1加密
  MessageDigest md = null;
  String result = "";
  try {
   md = MessageDigest.getInstance("SHA-1");
   byte[] data = md.digest(buff.toString().getBytes());
   
   //将字节数组转换成字符串
   result = bytesToString(data);
   
   System.out.println("加密后的字符串为:"+result);
   
  } catch (NoSuchAlgorithmException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  return result!=null?(result.equals(signature.toUpperCase())):false;
 }
 
 /**
  * 将字节数组转换成十六进制字符串
  * @param byteArray
  * @return
  */
 private static String bytesToString(byte[] byteArray){
  String stringDigest = "";
  for(int i=0;i<stringDigest.length();i++){
   stringDigest += byteToHexString(byteArray[i]);
  }
  return stringDigest;
 }
 
 /**
  * 将一个字节转换为十六进制字符串
  * @param mByte
  * @return
  */
 private static String byteToHexString(byte mByte){
  char[] digit = {&#39;0&#39;,&#39;1&#39;,&#39;2&#39;,&#39;3&#39;,&#39;4&#39;,&#39;5&#39;,&#39;6&#39;,&#39;7&#39;,&#39;8&#39;,&#39;9&#39;,&#39;A&#39;,&#39;B&#39;,&#39;C&#39;,&#39;D&#39;,&#39;E&#39;,&#39;F&#39;};
  char[] temp = new char[2];
  
  temp[0] = digit[(mByte>>>4) & 0X0F];
  temp[1] = digit[mByte & 0X0F];
  
  String str = new String(temp);
  return str;
 }
}
Copy after login

Do it on the first day With these, the function of WeChat request verification is completed.

【Related recommendations】

1. WeChat public account platform source code download

2. WeChat voting source code

The above is the detailed content of Secondary development of WeChat request verification. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template