1、使用java语言开发
2、服务器地址使用ngrok
3、gitlib管理代码(仓库地址在本文底部)
4、目前已完成的功能:模板推送、网页授权、群发图文消息、单图文消息、自定义菜单等。
ngrok -authtoken 复制的秘钥 -subdomain
1、双击.exe文件
2、输入命令8080为要映射的端口
ngrok http 8080
3、回车后如下(Forwarding 后面的域名可以在外网访问到本机8080端口)
ngrok by @inconshreveable (Ctrl+C to quit)
Session Status online
Account 1468216541 (Plan: Free)
Update update available (version 2.3.35, Ctrl-U to update
Version 2.3.34
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://b2953b06.ngrok.io -> http://localhost:8080
Forwarding https://b2953b06.ngrok.io -> http://localhost:8080
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
1、拿到aapId、appsecret和设置token、URL
2、编写后台接口
package com.wechat.demo.module.wechat.controller;
import com.wechat.demo.common.AuthorizationUtil;
import com.wechat.demo.constant.InvariableAttribute;
import com.wechat.demo.module.wechat.service.CoreSevice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 微信进行认证时请求login的get请求的接口认证通过后
* 所有操作会进入该请求的post请求中
* 所以处理用户订阅取消等一系列的操作在post请求中完成
* get只完成认证
*/
@Controller
@RequestMapping("/authorization")
public class AuthorizationController {
private final static Logger log = LoggerFactory.getLogger(AuthorizationController.class);
@Autowired
CoreSevice coreSevice;
@GetMapping("/login")
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
log.info("----开始请求检验-----");
String signature = request.getParameter("signature");
log.info("获取到<" + signature + ">");
String timestamp = request.getParameter("timestamp");
log.info("获取到<" + timestamp + ">");
String nonce = request.getParameter("nonce");
log.info("获取到<" + nonce + ">");
String echostr = request.getParameter("echostr");
log.info("获取到<" + echostr + ">");
PrintWriter print = response.getWriter();
if (AuthorizationUtil.CheckSignature(signature,timestamp,nonce)){
print.write(echostr);
}
print.flush();
print.close();
}
@PostMapping("/login")
public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter out = response.getWriter();
String result = coreSevice.pareRequest(request);
if(InvariableAttribute.RETURN_SUCCESS.equals(result)){
out.print(InvariableAttribute.RETURN_SUCCESS);
}else {
out.print(result);
}
out.flush();
out.close();
}
}
认证工具类
package com.wechat.demo.common;
import com.wechat.demo.constant.InvariableAttribute;
import java.security.MessageDigest;
import java.util.Arrays;
public class AuthorizationUtil {
//进行字典排序
public static boolean CheckSignature(String signature,String timestamp,String nonce){
String[] arr = new String[]{
InvariableAttribute.TOKEN,timestamp,nonce};
Arrays.sort(arr);
StringBuffer content = new StringBuffer();
for(int i = 0;i<arr.length;i++){
content.append(arr[i]);
}
String temp = getShal(content.toString());
return temp.equals(signature);
}
//进行sha1加密
public static String getShal(String str){
if(null ==str || 0 == str.length()){
return null;
}
char[] hexDigts = {
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
try {
MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
mdTemp.update(str.getBytes("UTF-8"));
byte[] md = mdTemp.digest();
int j = md.length;
char[] buf = new char[j*2];
int k = 0;
for(int i = 0 ; i < j ; i ++){
byte byte0 = md[i];
buf[k++] = hexDigts[byte0 >>> 4 & 0xf];
buf[k++] = hexDigts[byte0 & 0xf];
}
return new String(buf);
}catch (Exception e){
return null;
}
}
}
常量类
package com.wechat.demo.constant;
public class InvariableAttribute {
//设置的token
public static final String TOKEN = "自定义的token";
//appid
public static final String APPID = "填写获取的appid";
//appsecret
public static final String APPSECRET = "appsercret";
//接收微信服务器返回的请求成功的标志位
public static final String RETURN_SUCCESS = "success";
}
git仓库地址
微信开发文档地址
微信公众号的开发流程就是这些。源码在git仓库。
水平有限,写的不好的地方望指教。