这里开始开发啦,请主动打开发文档!
我们先了解一下系统的原理:(摘取于https://www.cnblogs.com/mydomain/p/8303736.html)
微信公众平台的数据交互线路一般按照“微信用户客户端—微信服务器(我的公众号)—网微站服务器(核心代码页)—微信服务器(我的公众号)—微信客户端”,每一次的数据交互都是通过用户发送消息进行请求。微信公众平台提供消息接口的行为标准是指导数据进行交互。如果微信公众号接入了消息接口,当用户在向公众号发送消息请求时,微信服务器将通过HTTP 请求传送给网站服务器,网站服务器会根据我们提前设置好的规则进行响应,并回复指定的操作或消息。
如果微信端,要与后台进行关联,那么当用户进行与后台交互的时候,后台就需要采取,身份验证,而这个是通过GET方式的请求,而只有通过的才可以进行后续的处理。则接入生效,成为开发者成功,否则接入失败。我们后端使用的是springboot项目(部分摘取于https://blog.csdn.net/Cs_hnu_scw/article/details/79080593),来来来,上代码!
/**
* 1)首先通过 request.getParameter 方法获取到微信服务器发来的 signature、timestamp、nonce 以及 echostr 四个参数,
* 这四个参数中:signature 表示微信加密签名,
* signature 结合了开发者填写的 token 参数和请求中的timestamp参数、nonce参数;
* timestamp 表示时间戳;nonce 表示随机数;
* echostr 则表示一个随机字符串。
* 2)开发者通过检验 signature 对请求进行校验,如果确认此次 GET 请求来自微信服务器,则原样返回 echostr 参数内容,则接入生效,成为开发者成功,否则接入失败。
* 3)具体的校验就是这里的 CheckUtil.checkSignature 方法,在这个方法中,首先将token、timestamp、nonce 三个参数进行字典序排序,
* 然后将三个参数字符串拼接成一个字符串进行 sha1 加密,最后开发者获得加密后的字符串可与 signature 对比,标识该请求来源于微信。
*
* */
@GetMapping("/verify_wx_token")
public void login(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
request.setCharacterEncoding("UTF-8");
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
PrintWriter out = null;
try {
out = response.getWriter();
if (CheckUtils.checkSignature(signature, timestamp, nonce)) {
out.write(echostr);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
out.close();
}
}
校验签名的的方法工具类
package com.example.boot.wx.access.utils;
import java.security.MessageDigest;
import java.util.Arrays;
public class CheckUtils {
private static final String token = "123456";
public static boolean checkSignature(String signature, String timestamp, String nonce) {
String[] str = new String[]{token, timestamp, nonce};
//排序
Arrays.sort(str);
//拼接字符串
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < str.length; i++) {
buffer.append(str[i]);
}
//进行sha1加密
String temp = SHA1.encode(buffer.toString());
//与微信提供的signature进行匹对
return signature.equals(temp);
}
}
向指定的地址发送Get请求的工具类
package com.example.boot.wx.access.utils;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* 向指定的地址发送Get请求
*/
public class GetMethod {
public static String getMethod(String url) throws IOException {
URL u = new URL(url);
URLConnection c = u.openConnection();
InputStream stream = c.getInputStream();
byte[] bytes = new byte[1024];
StringBuilder b = new StringBuilder();
int length;
while((length=stream.read(bytes))!=-1){
b.append(new String(bytes,0,length));
}
return b.toString();
}
}
SHA1加密工具类
package com.example.boot.wx.access.utils;
import java.security.MessageDigest;
/**
* 1)将token、timestamp、nonce三个参数进行字典序排序
* 2)将三个参数字符串拼接成一个字符串进行sha1加密
* 3)开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
* */
public class SHA1 {
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
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();
}
public static String encode(String str) {
if (str == null) {
return null;
}
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
messageDigest.update(str.getBytes());
return getFormattedText(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
最后我们返回到我们的微信公众号的测试开发平台上点击提交,如果成功就会提示我们配置成功,否则会提示配置失败。
配置成功后就说明我们已经接入微信公众号生效,成为了开发者。成为开发者后,用户每次向公众号发送消息、或者产生自定义菜单、或产生微信支付订单等情况时,开发者填写的服务器配置URL将得到微信服务器推送过来的消息和事件,开发者可以依据自身业务逻辑进行响应,如回复消息。