gitee项目地址:https://gitee.com/qlglg123/spring-tools-suite.git
上节写了新建公众号流程以及内网穿透工具的使用,以上准备工作整完就可以开始对微信公众平台的接口进行测试了。
1.新建web项目
引入web依赖
WeCharController测试
@RestController
@RequestMapping("/v1/weChart")
public class WeCharController {
private String token = "lizzuabc123123";
@Resource
AccessTokenService accessTokenService;
//接口测试
@PostMapping("/info")
public String getInfo(){
return "hello word ! ";
}
}
启动服务测试
接入微信平台
接入指南的步骤说明
接入概述
接入微信公众平台开发,开发者需要按照如下步骤完成:
1、填写服务器配置
2、验证服务器地址的有效性
3、依据接口文档实现业务逻辑
第一步:填写服务器配置
第二步:验证消息的确来自微信服务器
以上说明当你第一步配置了URL、token点击提交后,他会发送GET请求到你的服务器,携带signature、timestamp、nonce、echostr。
测试:
@GetMapping
public String getWeChar(@RequestParam String signature,
@RequestParam String timestamp,
@RequestParam String nonce,
@RequestParam String echostr) {
System.out.println("signature :" + signature);
System.out.println("timestamp :" + timestamp);
System.out.println("nonce :" + nonce);
System.out.println("echostr :" + echostr);
return echostr;
}
数据正常返回,
若确认此次 GET 请求来自微信服务器,请原样返回 echostr 参数内容,则接入生效,成为开发者成功,否则接入失败。
现在是没有增加校验直接返回了echostr。可以看到微信平台显示绑定成功
增加加密/校验流程
1)将token、timestamp、nonce三个参数进行字典序排序
2)将三个参数字符串拼接成一个字符串进行sha1加密
3)开发者获得加密后的字符串可与 signature 对比,标识该请求来源于微信
给的文档是用PHP实现的代码,我们需要用java代码实现
sha1加密实现工具类
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
创建访问认证工具类AccessAuthentication
import org.apache.commons.codec.digest.DigestUtils;
import java.io.Serializable;
import java.util.Arrays;
/**
* @author lizzu
* @date 2022/9/20 14:20
* @description
* 接入认证
**/
public class AccessAuthentication implements Serializable {
/**
* 将token、timestamp、nonce三个参数进行字典序排序
* 2)将三个参数字符串拼接成一个字符串进行sha1加密
* 3)开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
*/
private String token;
private String timestamp;
private String nonce;
private String signature;
private AccessAuthentication(String token, String timestamp, String nonce, String signature) {
this.token = token;
this.timestamp = timestamp;
this.nonce = nonce;
this.signature = signature;
}
public static AccessAuthentication of(String token, String timestamp, String nonce, String signature) {
return new AccessAuthentication(token, timestamp, nonce, signature);
}
/**
* 接入验证
*
* @return 是否验证通过
*/
public boolean checkSignature() {
// 1、将token、timestamp、nonce三个参数进行字典序排序
String[] strs = new String[]{this.token, this.timestamp, this.nonce};
Arrays.sort(strs);
// 将三个参数字符串拼接成一个字符串进行sha1加密
StringBuilder sb =new StringBuilder();
for (String str : strs) {
sb.append(str);
}
// String str = strs[0] + strs[1] + strs[2];
String str = sb.toString();
// sha1加密 计算 SHA-1 摘要并将值作为十六进制字符串返回。
String sha1Hex = DigestUtils.sha1Hex(str);
// 比较签名值是否一致
return sha1Hex.equals(this.signature);
}
}
增加校验功能
@GetMapping
public String getWeChar(@RequestParam String signature,
@RequestParam String timestamp,
@RequestParam String nonce,
@RequestParam String echostr) {
System.out.println("signature :" + signature);
System.out.println("timestamp :" + timestamp);
System.out.println("nonce :" + nonce);
System.out.println("echostr :" + echostr);
//增加校验功能
if (AccessAuthentication.of(token, timestamp, nonce, signature).checkSignature()) {
return echostr;
}
return null;
}
至此配置地址到微信公众平台完毕
下一篇 :
获取Access token