您的位置:首页 > 移动开发 > 微信开发

JAVA微信订阅号开发者模式接入(java微信开发学习笔记1)

2017-08-02 17:04 381 查看
这里说的是微信开发者模式接入,本地调试部分。
微信开发接入有两个条件:
第一,是必须是外网地址;
第二,是http必须是80端口,https必须是443端口。
我使用的是内网穿透的一个工具ngrok,我使用的国外的,不过速度挺快的,千万别使用花生壳,太。。。坑了。
ngrok地址:https://ngrok.com/

EncodingAESKey随机生成即可,我初学使用的是明文模式。
下面具体说一下服务器的代码。
微信的官方文档其实说的很清楚,我只是记录一下。
微信官方文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319
微信的验证消息是以get方式的请求传递过来的。
首先是接收微信传递过来的消息:


public class WeiXinServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub

String signature = req.getParameter("signature");
String timestamp = req.getParameter("timestamp");
String nonce = req.getParameter("nonce");
String echostr = req.getParameter("echostr");
System.out.println(signature + timestamp + nonce);
PrintWriter writer = resp.getWriter();
if (CheckUtil.checkSignature(signature, timestamp, nonce)) {
writer.print(echostr);
}
}
}


然后是对微信传过来的数据进行处理之后返回:


package com.weixin.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class CheckUtil {
private static final String token = "你的token";
public static boolean checkSignature(String signature,String timestamp,String nonce){
String[] arr = new String[]{token,timestamp,nonce};
//排序
Arrays.sort(arr);

//生成字符串
StringBuffer content = new StringBuffer();
for (int i = 0; i < arr.length; i++) {
content.append(arr[i]);
}

//sha1加密
String temp = getSha1(content.toString());
System.out.println("temp--->"+temp);
System.out.println("signature--->"+signature);
return temp.equals(signature);
}

public static String getSha1(String str){
if (null == str || 0 == str.length()){
return null;
}
char[] hexDigits = { '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++] = hexDigits[byte0 >>> 4 & 0xf];
buf[k++] = hexDigits[byte0 & 0xf];
}
return new String(buf);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}


最后是配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>WeiXin</display-name>

<servlet>
<servlet-name>weixinServlet</servlet-name>
<servlet-class>com.weixin.servlet.WeiXinServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>weixinServlet</servlet-name>
<url-pattern>/wx.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  微信 java