Home  >  Article  >  Java  >  Detailed explanation of Java+WeChat public account development process steps

Detailed explanation of Java+WeChat public account development process steps

php是最好的语言
php是最好的语言Original
2018-08-10 14:44:4512224browse

In the past two days, I wanted to learn WeChat public account development, so I searched online and started practicing. During the process, due to various problems, (the description was incomplete, some articles popped up a new constant without knowing how, It didn’t say where it was defined, and the problem with the jar package version cost me a day), so I will record it here.

1. First, you must register a WeChat public account. Go to Du Niang to search the WeChat public platform and enter this page to register (skip this step if you already have an account):

Detailed explanation of Java+WeChat public account development process steps

2. Then use Eclipse to create a new project. Here I am building a web project, jdk is 1.8, and tomcat is 8.5.

Detailed explanation of Java+WeChat public account development process steps

3 .In order to establish a connection with WeChat, after building the project, first create a new class and name it: CheckUtil. Its function is to serve as a verification tool for connecting to WeChat. The code is as follows:

import java.security.MessageDigest;
import java.util.Arrays;

public class CheckUtil {
	public static final String  tooken = "自行定义"; //开发者自行定义Tooken
	public static boolean checkSignature(String signature,String timestamp,String nonce){
		//1.定义数组存放tooken,timestamp,nonce
		String[] arr = {tooken,timestamp,nonce};
		//2.对数组进行排序
		Arrays.sort(arr);
		//3.生成字符串
		StringBuffer sb = new StringBuffer();
		for(String s : arr){
			sb.append(s);
		}
		//4.sha1加密,网上均有现成代码
		String temp = getSha1(sb.toString());
		//5.将加密后的字符串,与微信传来的加密签名比较,返回结果
		return temp.equals(signature);
	}

	public static String getSha1(String str){
        if(str==null||str.length()==0){
            return null;
        }
        char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
                'a','b','c','d','e','f'};
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));
            byte[] md = mdTemp.digest();
            int j = md.length;
            char buf[] = new char[j*2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];      
            }
            return new String(buf);
        } catch (Exception e) {
            // TODO: handle exception
            return null;
        }
    }
}

4. Then create a new servlet , rewrite the doGet method to receive the GET request from WeChat. Part of the code is as follows:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		String signature = request.getParameter("signature");
		String timestamp = request.getParameter("timestamp");
		String nonce = request.getParameter("nonce");
		String echostr = request.getParameter("echostr");
		PrintWriter out = response.getWriter();
		if(CheckUtil.checkSignature(signature, timestamp, nonce)){
			//如果校验成功,将得到的随机字符串原路返回
			out.print(echostr);
		}
	}

5. After the code is written, we need a tool to map our intranet link to the public network so that WeChat can access it. Go to our backend. Here I use ngrok, a free mapping tool. Just go to Du Niang to search and download it. After downloading, unzip it and put it in a designated location. Press the Win key and R key at the same time, enter cmd, and press Enter to enter. Dos environment, switch to the drive letter where ngrock is located, enter ngrock http 8080 and press Enter (start tomcat before doing this):

Detailed explanation of Java+WeChat public account development process steps

6. Press Enter and wait for a while, that is You can get the public network link. The link given in the shaded area shown in the figure below can directly access the link content under the local machine 127.0.0.1:8080, which are the addresses corresponding to the http protocol and https protocol:

Detailed explanation of Java+WeChat public account development process steps

7. Log in to the WeChat public account platform, slide to the bottom, and click Development in the lower left corner - Basic configuration:

Detailed explanation of Java+WeChat public account development process steps

Click the submit button , the prompt submission is successful, which means that WeChat can access our own backend.

Related recommendations:

Java implements graphic and text code examples for WeChat public platform development

WeChat public account payment development (java) examples Detailed explanation

The above is the detailed content of Detailed explanation of Java+WeChat public account development process steps. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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