WeChat public account platform builds connection javaweb

  • prerequisites:

    • A locally runnable javaweb: mine is an online git springboot project
    • Register a WeChat official account application link
  • Preparation before configuration - intranet penetration

    Since the project is built on a local computer and cannot be accessed from the external network, a tool needs to be used to map the local address to the public network. Use of free tools: natapp
    natapp download website
    natapp configuration tutorial
    Note: Only port 80 can be used here, because the WeChat official account is only open to port 80.

Free Tunnel Configuration
Register first, and log in after successful registration.
enter description here
enter description here
After the tunnel purchase is successful, you can see the existing tunnel in my tunnel:
enter description here
client download
We visit the natapp client download, download the natapp client:
enter description here
after downloading, unzip, there will be a natapp.exe file.

Before running
natapp, you need to configure it before running natapp. For detailed tutorials, please refer to: Using the local configuration file config.ini .
config.ini content:
enter description here
Note: The config.ini configuration file needs to be in the same directory as natapp.exe.
enter description here

Connection
After starting the javaweb project locally: access localhost:80,
enter description here
double-click natapp.exe to run, and access your own connectionhttp://wge5gq.natappfree.cc/:80
enter description here

Note: Since I am using springboot here, the default port is 8080, so I need to modify the port configuration in the configuration file application

  • Shut down the iis server: Internet Information Services
    fails to configure because the iis of the win10 system occupies port 80 and needs to be closed
    Steps : Control Panel -> Programs -> Enable or Disable Windows Features -> Cancel the 'Internet Information Services' option -> OK

  • request verification

    The WeChat public platform needs to send a request to the server to confirm the server connection.
    This requires adding a separate request connection to the project

    1. several parameters
      enter description here

    2. Request verification code writing

    • controller类:HelloWorldController.class
    • Check interface consistency tool class: CheckoutUtil.class
package cn.ictgu.tools;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author: ZouTai
 * @date: 2018/5/3
 * @description:
 */
public class CheckoutUtil {
    // 与接口配置信息中的Token要一致
    private static String token = "TOKEN";

    /**
     * 验证签名
     *
     * @param signature
     * @param timestamp
     * @param nonce
     * @return
     */
    public static boolean checkSignature(String signature, String timestamp, String nonce) {
        String[] arr = new String[] { token, timestamp, nonce };
        // 将token、timestamp、nonce三个参数进行字典序排序
        // Arrays.sort(arr);
        sort(arr);
        StringBuilder content = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            content.append(arr[i]);
        }
        MessageDigest md = null;
        String tmpStr = null;

        try {
            md = MessageDigest.getInstance("SHA-1");
            // 将三个参数字符串拼接成一个字符串进行sha1加密
            byte[] digest = md.digest(content.toString().getBytes());
            tmpStr = byteToStr(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        content = null;
        // 将sha1加密后的字符串可与signature对比,标识该请求来源于微信
        return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
    }

    /**
     * 将字节数组转换为十六进制字符串
     *
     * @param byteArray
     * @return
     */
    private static String byteToStr(byte[] byteArray) {
        String strDigest = "";
        for (int i = 0; i < byteArray.length; i++) {
            strDigest += byteToHexStr(byteArray[i]);
        }
        return strDigest;
    }

    /**
     * 将字节转换为十六进制字符串
     *
     * @param mByte
     * @return
     */
    private static String byteToHexStr(byte mByte) {
        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] tempArr = new char[2];
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
        tempArr[1] = Digit[mByte & 0X0F];
        String s = new String(tempArr);
        return s;
    }
    public static void sort(String a[]) {
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[j].compareTo(a[i]) < 0) {
                    String temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    }
}



package cn.ictgu.tools;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author: ZouTai
 * @date: 2018/5/3
 * @description:
 */
public class CheckoutUtil {
    // 与接口配置信息中的Token要一致
    private static String token = "TOKEN";

    /**
     * 验证签名
     *
     * @param signature
     * @param timestamp
     * @param nonce
     * @return
     */
    public static boolean checkSignature(String signature, String timestamp, String nonce) {
        String[] arr = new String[] { token, timestamp, nonce };
        // 将token、timestamp、nonce三个参数进行字典序排序
        // Arrays.sort(arr);
        sort(arr);
        StringBuilder content = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            content.append(arr[i]);
        }
        MessageDigest md = null;
        String tmpStr = null;

        try {
            md = MessageDigest.getInstance("SHA-1");
            // 将三个参数字符串拼接成一个字符串进行sha1加密
            byte[] digest = md.digest(content.toString().getBytes());
            tmpStr = byteToStr(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        content = null;
        // 将sha1加密后的字符串可与signature对比,标识该请求来源于微信
        return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
    }

    /**
     * 将字节数组转换为十六进制字符串
     *
     * @param byteArray
     * @return
     */
    private static String byteToStr(byte[] byteArray) {
        String strDigest = "";
        for (int i = 0; i < byteArray.length; i++) {
            strDigest += byteToHexStr(byteArray[i]);
        }
        return strDigest;
    }

    /**
     * 将字节转换为十六进制字符串
     *
     * @param mByte
     * @return
     */
    private static String byteToHexStr(byte mByte) {
        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] tempArr = new char[2];
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
        tempArr[1] = Digit[mByte & 0X0F];
        String s = new String(tempArr);
        return s;
    }
    public static void sort(String a[]) {
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[j].compareTo(a[i]) < 0) {
                    String temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    }
}
  • Official account server configuration
    Fill in the URL and the token corresponding to the code certificate, and click Enable
    enter description here
    Possible errors: access timeout, token verification error The
    verification error may be that the token is inconsistent or cannot be accessed. You need to check whether the intranet penetration is correct. The
    access timeout may be natapp Free usage doesn't work at some point, so you can delete, reapply for free, and try again

Reference link:
Intranet penetration
request verification

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326399726&siteId=291194637