微信测试帐号如何设置URL和Token,以及相关验证的原理
2024-08-24 07:31:17
首先说明,本帮助文档是利用javaweb的Servlet来进行“接口配置信息配置信息”认证的。
在学习微信公众号开发的时候,读到填写服务器配置的帮助部分,总是不能理解为啥按照他的步骤做总是设置失败(吐槽:这个帮助写的太简略,原理讲了一般,对不了解PHP的人简直就是歧视)。
设置失败的一个重要原因:url的请求相应没有返回echostr这个参数。
如果读到这里你还不能理解,就请看以下的截图和源代码:
1、首先我搭建了一个将局域网内我的电脑发布到intetnet上的环境,利用ngrok,如果想了解如何搭建,请查看我的另一篇博客:ngrok使用命令帮助
@WebServlet("/portal")
public class ServerPortal extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String token = "janken"; /**
* @see HttpServlet#HttpServlet()
*/
public ServerPortal() {
super();
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
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");
System.out.println("signature:" + signature);
System.out.println("timestamp:" + timestamp);
System.out.println("nonce:" + nonce);
System.out.println("echostr:" + echostr);
PrintWriter pw = response.getWriter();
pw.append(echostr);
pw.flush();
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }
4、根据ngrok提供的url和我们创建的servlet填写微信的url和token
5、点击提交,这是微信会主动访问我们写的servlet,获得我们返回的参数中的echostr的内容即可认证通过。
认证过程原理分析:
最新文章
- 1ms引发的问题
- 第二次团队作业——预则立&;&;他山之石
- python 函数的参数定义及调用
- 上传文件大于 2G以上
- 现在, Delphi 的多线程已经非常易用了!
- C#中jQuery Ajax实例(二)
- Be a person
- XCode 项目配置说明
- cell的循环使用
- Oracle中的单引号问题
- 在C++程序中自动加入svn版本号
- iOS 开发笔记 - 开发中如何实现自动检测更新APP
- HDFS 的垃圾回收配置
- javascript性能优化之使用对象、数组直接量代替典型的对象创建和赋值
- JS拖拽元素原理及实现代码
- 3.配置Spring+SpringMvc+Mybatis(分库or读写分离)--Intellij IDAE 2016.3.5
- windows批处理命令
- SQL Server 调优系列进阶篇 - 如何维护数据库索引
- 【无图慎入】Link Cut Tree 总结
- 【洛谷】1494:[国家集训队]小Z的袜子【莫队】