文章是根据本人根据慕课网的视频教程自己弄了一次,亲测成功。
1、首先在eclipse创建一个web工程,tomcat启动后可以通过访问index.jsp是否成功。
2、下载一个公网映射工具“grnok.exe”,接下来参考http://www.tunnel.mobi/里面的方法。下载ngrok.cfg文件,通过cmd.exe进行启动grnok。
输入dos命令“ngrok -config ngrok.cfg -subdomain weixin 7001”,其中weixin 自定义名称,7001为tomcat端口。执行后会显示如下图。
<img src="https://img-blog.youkuaiyun.com/20151005094736722?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
<span style="font-family: Arial, Helvetica, sans-serif;">3、在web工程中新建一个servlet来接收公众号服务器发来的请求信息,参考公众号的“开发者中心=》接入指南”。其中代码参考如下:</span>
代码1
public class WeixinServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
<span style="white-space:pre"> </span>throws ServletException, IOException {
<span style="white-space:pre"> </span>String signature = request.getParameter("signature");
<span style="white-space:pre"> </span>String timestamp = request.getParameter("timestamp");
<span style="white-space:pre"> </span>String nonce = request.getParameter("nonce");
<span style="white-space:pre"> </span>String echostr = request.getParameter("echostr");
<span style="white-space:pre"> </span>PrintWriter out = response.getWriter();
<span style="white-space:pre"> </span>if (CheckUtil.checkSignature(signature, timestamp, nonce)) {
<span style="white-space:pre"> </span>out.print(echostr);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>out.close();
<span style="white-space:pre"> </span>}
}
代码2 检验公众号请求工具类
public class CheckUtil {
private static final String token = "aineko";
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 = SHA1.getSha1(content.toString());
return temp.equals(signature);
}
}
代码3 SHA1加密方法
public class SHA1 {
public static String getSha1(String str) {
<span style="white-space:pre"> </span>if (null == str || 0 == str.length()) {
<span style="white-space:pre"> </span>return null;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
<span style="white-space:pre"> </span>'a', 'b', 'c', 'd', 'e', 'f' };
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
<span style="white-space:pre"> </span>mdTemp.update(str.getBytes("UTF-8"));
<span style="white-space:pre"> </span>byte[] md = mdTemp.digest();
<span style="white-space:pre"> </span>int j = md.length;
<span style="white-space:pre"> </span>char[] buf = new char[j * 2];
<span style="white-space:pre"> </span>int k = 0;
<span style="white-space:pre"> </span>for (int i = 0; i < j; i++) {
<span style="white-space:pre"> </span>byte byte0 = md[i];
<span style="white-space:pre"> </span>buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
<span style="white-space:pre"> </span>buf[k++] = hexDigits[byte0 & 0xf];
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return new String(buf);
<span style="white-space:pre"> </span>} catch (NoSuchAlgorithmException e) {
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>return null;
<span style="white-space:pre"> </span>} catch (UnsupportedEncodingException e) {
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>return null;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
代码4 web.xml 配置servlet
<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><span style="white-space:pre"> </span>
4、然后在开发者中心中配置,然后提交,如图
完成以上5步,Java微信公众号开发--开发环境的搭建 完成。