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

Detailed explanation of Java+WeChat public account development process steps

Aug 10, 2018 pm 02:44 PM

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

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;

        }

    }

}

Copy after login

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

1

2

3

4

5

6

7

8

9

10

11

12

13

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);

        }

    }

Copy after login

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

See all articles