一、环境搭建
1.建议申请测试号进行开发,原因测试号接口比较全面
申请地址: 微信公众平台
2.使用ngrok将本地地址映射到公网,用于微信验证开发者服务器
ngrok下载地址:ngrok - secure introspectable tunnels to localhost
二、验证环境有效性
环境搭建好以后,新建一个springboot的web工程,测试环境的有效性:
1.工程的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.新建一个参数类,用于保存公众号的appID,appsecret,Token(Token是用户定义的值)
3.新建一个工具类,用于加密,获取,验证微信端的密文,验证开发者服务器
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class EncryptionUtil {
private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
/**
* 加密
* @param algorithm 算法名称
* @param body 加密的字符串
* @return 加密过后的字符串
*/
public static String encrypt(String algorithm,String body){
if(algorithm==null||body==null){
return null;
}
try {
//这是java自带的加密工具
MessageDigest digest = MessageDigest.getInstance(algorithm);
digest.update(body.getBytes());
return getFormattedText(digest.digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
private static String getFormattedText(byte[] bytes) {
int len = bytes.length;
StringBuilder buf = new StringBuilder(len * 2);
// 把密文转换成十六进制的字符串形式
for (int j = 0; j < len; j++) {
buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
}
return buf.toString();
}
}
4.新增接口类,以验证请求
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
@RestController
@RequestMapping("/token")
public class WeChatUtilController extends HttpServlet {
private String signature;
private String timestamp;
private String nonce;
private String echostr;
@GetMapping("/get")
public String doGet(){
System.out.println("========================");
return "测试微信";
}
/**
* 验证微信消息
*
* @param request
* @return
*/
@GetMapping("/check")
public String checkWxMsg(HttpServletRequest request) {
Logger logger= LoggerFactory.getLogger(WeChatUtilController.class);
/**
* 微信加密签名
*/
String signature = request.getParameter("signature");
/**
* 随机字符串
*/
String echostr = request.getParameter("echostr");
/**
* 时间戳
*/
String timestamp = request.getParameter("timestamp");
/**
* 随机数
*/
String nonce = request.getParameter("nonce");
String[] str={timestamp,nonce,WxConstants.TOKEN};
//将token、timestamp、nonce三个参数进行字典序排序
Arrays.sort(str);
StringBuffer sb = new StringBuffer();
//将三个参数字符串拼接成一个字符串进行sha1加密
for (String param:str) {
sb.append(param);
}
//获取到加密过后的字符串
String encryptStr = EncryptionUtil.encrypt("SHA1", sb.toString());
//判断加密后的字符串是否与微信的签名一致
if(signature.equalsIgnoreCase(encryptStr)){
return echostr;
}
logger.error("这不是微信发来的消息!!");
return "失败!!!";
}
}
6.通过测试号平台进行验证