- 在nat上面添加映射
- 在微信公众平台注册账号,并配置信息
- 接入认证
package com.asliu.wechat.controller;
import com.asliu.wechat.util.SecurityUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
/**
* 接入认证controller
*/
@RestController
public class AccessAuthenticationController {
//当在接入认证界面填写了对了信息后,点击提交。会携带四个参数一起发get请求到这个地址
@GetMapping("/index") //接入认证服务 http://530b0ed5.nat123.cc/index
public String accessAuthentication(HttpServletRequest request){
//signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
String signature = request.getParameter("signature");
//timestamp 时间戳
String timestamp = request.getParameter("timestamp");
//nonce 随机数
String nonce = request.getParameter("nonce");
//echostr 回显字符串
String echostr = request.getParameter("echostr");
System.out.println(signature);
System.out.println(timestamp);
System.out.println(nonce);
System.out.println(echostr);
// 1)将token、timestamp、nonce三个参数进行字典序排序(数组排序)
String token = "lzjtest";
String[] params = {token,timestamp,nonce};
Arrays.sort(params);
// 2)将三个参数字符串拼接成一个字符串进行sha1加密
StringBuilder sb = new StringBuilder();
for (String param : params) {
sb.append(param);
}
String content = sb.toString();
String securityContent = SecurityUtil.sha1(content);
// 3)开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
if (signature.equals(securityContent)){
//加密成功
return echostr;
}else{
//加密失败
throw new RuntimeException("加密失败!");
}
}
}
- 使用微信测试公众号就可以测试数据
-
关注响应
-
公众号菜单
-
ctrl+shift+X 大写英文
-
公众号的绑定