个人账号不能定义url访问服务器,使用测试号就不用认证添加url了,进入公众平台测试账号
开发服务器
###域名验证
进入公众平台测试账号
![](https://img2018.cnblogs.com/blog/1475969/201812/1475969-20181206225540256-973087606.png)
填写服务器url,和自己定义的token
![](https://img2018.cnblogs.com/blog/1475969/201812/1475969-20181206225811153-1694721774.jpg)
要求url为外网地址,所以要做内网到外网的转换,可以通过gnrok等软件实现
具体实现
服务器端代码
servlet
private final String token ="zhangzichun";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//response.getWriter().print("HHH");
System.out.println("开始签名校验");
//得到服务器传过来的4个参数
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
ArrayList<String> array = new ArrayList<String>();
array.add(signature);
array.add(timestamp);
array.add(nonce);
// 1.将token、timestamp、nonce三个参数进行字典序排序
String sortString = Sort.sort(token, timestamp, nonce);
// 2. 将三个参数字符串拼接成一个字符串进行sha1加密
String mytoken = Sort.SHA1(sortString);
// 3.将sha1加密后的字符串可与signature对比,标识该请求来源于微信
if (mytoken != null && mytoken != "" && mytoken.equals(signature)) {
System.out.println("签名校验通过。");
response.getWriter().println(echostr); //如果检验成功输出echostr,微信服务器接收到此输出,才会确认检验完成。
} else {
System.out.println("签名校验失败。");
}
//response.getWriter().print("jjjj");
}
排序方法和SHA1加密
public class Sort {
/**
* 排序方法
* @param token
* @param timestamp
* @param nonce
* @return
*/
public static String sort(String token, String timestamp, String nonce) {
String[] strArray = { token, timestamp, nonce };
Arrays.sort(strArray);
StringBuilder sbuilder = new StringBuilder();
for (String str : strArray) {
sbuilder.append(str);
}
return sbuilder.toString();
}
/**
* SHA1加密
* @param token
* @param timestamp
* @param nonce
* @return
*/
public static String SHA1(String decript) {
try {
MessageDigest digest = MessageDigest
.getInstance("SHA-1");
digest.update(decript.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexString = new StringBuffer();
// 字节数组转换为 十六进制 数
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
服务器与平台的token相同则通过
获取access_token
进入微信公众平台接口调试工具
选择基础支持,填写划红线的两个信息后点击验证,通过后得到access_token
自定义菜单
json格式填写如下
{
"button": [
{
"type": "view",
"name": "同学录",
"url": "http://289c6b58.ngrok.io/classmate/index.jsp"
},
{
"type": "view",
"name": "查看",
"url": "http://289c6b58.ngrok.io/classmate/index.jsp"
},
{
"type": "view",
"name": "编辑",
"url": "http://289c6b58.ngrok.io/classmate/index.jsp"
}
]
}
点击测试通过后,回到测试号管理界面
扫描二维码进行查看