微信公众平台开发(5)--验证消息的确来自微信服务器

简介: 本文目录1. 前言2. 构建项目2.1 构建SpringBoot项目2.2 编写配置文件2.3 编写启动类3. 验证消息来自微信3.1 获取公众号参数3.2 开发公众平台配置类3.3 验证消息方法的调用4. 小结

1. 前言

上一篇中,我们已经成功配置并启用了服务器信息,微信官方已经任何我们的服务器是合法的了。


但是这中间还有一个问题,就是我们如何能确认我们收到的消息是合法的呢,也就是说我们如何确认我们收到的消息是微信发出的而不是伪造的?


这就需要我们验证消息是否的确来自微信服务器了。


2. 构建项目

我们需要一个项目,将微信开发相关的代码放到里面。


2.1 构建SpringBoot项目

使用Maven构建一个Spring Boot项目,然后pom.xml配置如下。


需要注意的是,我们引入了weixin-java-mp,这是开源的封装好的微信JavaSDK,GitHub高星项目,我们引入该SDK可以大大提高我们的开发效率,不用从头造轮子了。


<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>

 <groupId>org.springframework.boot</groupId>

 <artifactId>spring-boot-starter-parent</artifactId>

 <version>2.2.5.RELEASE</version>

 <relativePath /> <!-- lookup parent from repository -->

</parent>

<groupId>cn.pandabrother</groupId>

<artifactId>wx-server</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>war</packaging>

<properties>

 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

 <java.version>1.8</java.version>

 <maven-jar-plugin.version>3.0.0</maven-jar-plugin.version>

</properties>

<dependencies>

 <dependency>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-web</artifactId>

 </dependency>

 <dependency>

  <groupId>org.projectlombok</groupId>

  <artifactId>lombok</artifactId>

 </dependency>

 <dependency>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-devtools</artifactId>

 </dependency>

 <!-- 微信公众号 -->

 <dependency>

  <groupId>com.github.binarywang</groupId>

  <artifactId>weixin-java-mp</artifactId>

  <version>4.1.0</version>

 </dependency>

</dependencies>

<build>

 <plugins>

  <plugin>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-maven-plugin</artifactId>

  </plugin>

 </plugins>

</build>

</project>

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

47

48

49

2.2 编写配置文件

编写applicaiton.yml配置文件如下,所以我们项目从80端口启动,且访问项目的根路径为/wx-server


server:

  port: 80 #端口

  servlet:

     context-path: /wx-server

1

2

3

4

2.3 编写启动类

启动类编写如下,用于快速启动我们的项目:


/**

* SpringBoot启动类

*/

@SpringBootApplication

public class WxServerApplication {

public static void main(String[] args) {

 SpringApplication.run(WxServerApplication.class, args);

}

}

1

2

3

4

5

6

7

8

9

3. 验证消息来自微信

3.1 获取公众号参数

进入【基本配置】菜单,记忆以下参数,这些参数需要配置到我们的代码中。



3.2 开发公众平台配置类

开发微信公众平台配置类如下,注意需要将上图中的参数填入下面的代码中。


该类有两个用途,第一个是注册了wxMpDefaultConfigImpl组件,该组件保存微信公众平台的参数。


第二个是注册了wxMpService组件,该组件用于调用封装好的方法,例如本文需要的验证消息的方法。


/**

* 微信公众平台配置

*/

@Configuration

public class WxMpConfig {


@Bean

public WxMpDefaultConfigImpl wxMpDefaultConfigImpl() {

 WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();

 config.setAppId(""); // 设置微信公众号的appid

 config.setSecret(""); // 设置微信公众号的app corpSecret

 config.setToken(""); // 设置微信公众号的token

 config.setAesKey(""); // 设置微信公众号的EncodingAESKey

 return config;

}


@Bean

public WxMpService wxMpService() {

 WxMpService wxMpService = new WxMpServiceImpl();// 实际项目中请注意要保持单例,不要在每次请求时构造实例,具体可以参考demo项目

 wxMpService.setWxMpConfigStorage(wxMpDefaultConfigImpl());

 return wxMpService;

}

}


3.3 验证消息方法的调用

自动注入wxMpService,然后调用其方法验证消息是否来自微信即可。


/**

* 验证控制器

*/

@Controller

public class CheckController {

@Autowired

private WxMpService wxMpService;


// 接入验证

@RequestMapping("/checkToken")

@ResponseBody

public String checkToken(@RequestParam("signature") String signature, @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce, @RequestParam("echostr") String echostr) {

 if (!wxMpService.checkSignature(timestamp, nonce, signature)) {

  // 消息不合法

  return "消息不合法";

 }

 // 消息合法

 return echostr;

}

}


通过调用checkSignature方法,我们就能确定消息是来自微信,而不是恶意伪装的了。如果要保证安全的话,所有来自微信的消息,都应该经过验证,而不仅仅是针对与接入验证这一种消息。


4. 小结

本文介绍了如何快速利用WxJava这一开源SDK,快速实现验证消息来自微信服务器的功能。

相关文章
|
22小时前
|
小程序 开发者
uniapp实战 —— 开发微信小程序的调试技巧
uniapp实战 —— 开发微信小程序的调试技巧
7 1
|
1天前
|
小程序
【微信小程序-原生开发】富文本编辑器 editor 的使用教程
【微信小程序-原生开发】富文本编辑器 editor 的使用教程
8 0
【微信小程序-原生开发】富文本编辑器 editor 的使用教程
|
1天前
|
存储 小程序 API
【微信小程序-原生开发+云开发+TDesign】修改用户头像(含wx.chooseMedia,wx.cloud.uploadFile,wx.cloud.deleteFile的使用)
【微信小程序-原生开发+云开发+TDesign】修改用户头像(含wx.chooseMedia,wx.cloud.uploadFile,wx.cloud.deleteFile的使用)
4 0
【微信小程序-原生开发+云开发+TDesign】修改用户头像(含wx.chooseMedia,wx.cloud.uploadFile,wx.cloud.deleteFile的使用)
|
23小时前
|
小程序 前端开发
【微信小程序-原生开发】添加自定义图标(以使用阿里图标库为例)
【微信小程序-原生开发】添加自定义图标(以使用阿里图标库为例)
11 0
|
1天前
|
小程序
【微信小程序-原生开发】客服
【微信小程序-原生开发】客服
9 0
|
1天前
|
小程序
【微信小程序-原生开发】wxml 支持 includes (wxml中执行函数的方法)
【微信小程序-原生开发】wxml 支持 includes (wxml中执行函数的方法)
7 0
|
1天前
|
小程序 JavaScript
【微信小程序-原生开发】watch 的实现
【微信小程序-原生开发】watch 的实现
6 0
|
1天前
|
小程序 JavaScript
【微信小程序-原生开发】实用教程11 - 用户登录鉴权(含云函数的创建、删除、使用,通过云函数获取用户的openid)
【微信小程序-原生开发】实用教程11 - 用户登录鉴权(含云函数的创建、删除、使用,通过云函数获取用户的openid)
6 0
|
1天前
|
缓存 小程序
【微信小程序-原生开发】启动时自动升级更新到最新版本
【微信小程序-原生开发】启动时自动升级更新到最新版本
5 0
|
1天前
|
小程序 定位技术
【微信小程序-原生开发+TDesign】地图导航(wx.openLocation的使用)
【微信小程序-原生开发+TDesign】地图导航(wx.openLocation的使用)
5 0