运用Java对微信公众平台二次开发技术——开发者模式接入
当初我在这碰到了很多问题,市面上以及网络上的资料特别少,所以当初碰了很多壁,所以现在跟大家分享一下,如何用Java,对微信公众平台进行二次开发。
一、开发预备知识:
最基本的JavaSE与JavaWeb知识:JSP/Servlet/JDBC/EL
二、开发环境
Eclipse EE
JDK 1.7(用JDK1.8会报错!用JDK1.8会报错!用JDK1.8会报错!用JDK1.8会报错!重要的事情说4遍,当初被坑了,愣是不知道错在哪,检查了无数遍代码,就是不知道哪里错了)
三、注册微信号
http://mp.weixin.qq.com/
具体过程略,千万别乱注册多个,貌似一个身份证只能注册管理5个微信公众号。
四、建立项目
1、打开eclipse
2、新建一个 Dynamic Web Project
3、新建一个servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | package org.hjj.servlet; import java.io.IOException; import java.io.PrintWriter; import java.io.Writer; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.hjj.service.CoreService; import org.hjj.util.CheckUtil; @WebServlet ( "/wx.do" ) public class WeixinServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String signature = request.getParameter( "signature" ); // 微信加密签名 String timestamp = request.getParameter( "timestamp" ); // 时间戳 String nonce = request.getParameter( "nonce" ); // 随机数 String echostr = request.getParameter( "echostr" ); // 随机字符串 Writer out = response.getWriter(); System.out.println( "收到验证请求:" ); System.out.println( " signature=" +signature); System.out.println( " timestamp=" +timestamp); System.out.println( " nonce=" +nonce); System.out.println( " echostr=" +echostr); if (signature== null || timestamp== null || nonce== null || echostr== null ){ //这几个参数为空时,排序会报错。 out.write( "parameter is null!" ); System.out.println( "failed" ); } else { if (CheckUtil.checkSignature(signature, timestamp, nonce)) { out.write(echostr); // 请求验证成功,返回随机码 System.out.println( "success" ); } else { System.out.println( "check failed" ); out.write( "check error!" ); } } out.flush(); out.close(); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding( "UTF-8" ); response.setCharacterEncoding( "UTF-8" ); PrintWriter out = response.getWriter(); String respXml = CoreService.processRequest(request); out.print(respXml); out.close(); out= null ; } } |
上面的dopost方法是用来回复消息的,初期可以不使用,可以先删了dopost内的方法
另外在 Util包内编写验证和加密逻辑
这个是验证的逻辑.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package org.hjj.util; 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]); } String temp = SHA1.encode(content.toString()); return temp.equals(signature); } } |
加密逻辑我采用的是sha1加密,也很方便,网上有很多案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package org.hjj.util; import java.security.MessageDigest; /** * <p>Title: SHA1算法</p> * * @author qsyang<yangqisheng274@163.com> */ public final class SHA1 { private static final char [] HEX_DIGITS = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' }; /** * Takes the raw bytes from the digest and formats them correct. * * @param bytes the raw bytes from the digest. * @return the formatted bytes. */ 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为:服务器名+项目名+servlet(我这里是wx.do)
token自己定义
AESKey自己定义,
然后点确定就可以发布了
我们工作室租用的是阿里云服务器,所以部署一下就可以直接用,当然我们学生如果没有服务器条件的,可以使用ngrok工具,将自己本地ip映射到公网上去。(找不到博客园的上传文件功能,同学们可以自己去搜)
使用方法是 用cmd把路径设置到ngrok.exe的目录下
输入:ngrok -config ngrok.cfg -subdomain 自己取一个串替换这句话 8080
回车,
自己的ip就到公网上了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?