不少人刚刚看是看源码的时候,都会想到一个问题,就是说,做者这些个思路都是从哪儿来的?为何他有思路,我就没有思路,是否是我本身就比别人要笨了?其实这个问题,我也思考过好久,直到我看到这个,我才发现,不是我笨,是我眼界不够开阔。html
全部的Jfinal微信sdk的方法和理论依据,他都是来自https://mp.weixin.qq.com/wiki/home/index.html,因此,当你结合这个文档看的时候,你会发现,做者仅仅是用本身的方式将这里的接口请求包装了一层,让咱们不用太关心底层的实现而又可以完成咱们的开发任务。java
一、填写服务器配置服务器
二、验证服务器地址的有效性微信
三、依据接口文档实现业务逻辑框架
(不懂的本身去看文档)测试
在这里,咱们关键是去看第二步,这一个是咱们今天的重点,有人说,这个过程我知道,你不用给我说,我只想问你一句,假如我让你实现这么个过程,你怎么作?我如今就掰开Jfinalweixin这个开源框架的代码,脱光了给你看,省得大家说我忽悠你。加密
开发者提交信息后,微信服务器将发送GET请求到填写的服务器地址URL上,GET请求携带四个参数:spa
signature线程
微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。code
timestamp
时间戳
nonce
随机数
echostr
随机字符串
开发者经过检验signature对请求进行校验(下面有校验方式)。若确认这次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功,不然接入失败。
加密/校验流程以下:
1. 将token、timestamp、nonce三个参数进行字典序排序
2. 将三个参数字符串拼接成一个字符串进行sha1加密
3. 开发者得到加密后的字符串可与signature对比,标识该请求来源于微信
经过以上过程,咱们能够猜出来,咱们须要接受上述的四个参数,而后对其进行加密,而后在进行比对,为何要这样?就是为了肯定,这个信息,他真的是从微信来的,不从什么别的什么异性来的?
什么?你不信?别急,准备好锄头和黑驴蹄子,兄弟我带你去挖坟盗墓!!!
MsgInterceptor.java public void intercept(Invocation inv) { Controller controller = inv.getController(); if (controller instanceof MsgController == false) throw new RuntimeException("控制器须要继承 MsgController"); try { // 将 ApiConfig 对象与当前线程绑定,以便在后续操做中方便获取该对象: ApiConfigKit.getApiConfig(); ApiConfigKit.setThreadLocalApiConfig(((MsgController)controller).getApiConfig()); // 若是是服务器配置请求,则配置服务器并返回 if (isConfigServerRequest(controller)) { configServer(controller); return ; } // 对开发测试更加友好 if (ApiConfigKit.isDevMode()) { inv.invoke(); } else { // 签名检测 if (checkSignature(controller)) { inv.invoke(); } else { controller.renderText("签名验证失败,请肯定是微信服务器在发送消息过来"); } } } finally { ApiConfigKit.removeThreadLocalApiConfig(); } } /** * 检测签名 */ private boolean checkSignature(Controller controller) { String signature = controller.getPara("signature"); String timestamp = controller.getPara("timestamp"); String nonce = controller.getPara("nonce"); if (StrKit.isBlank(signature) || StrKit.isBlank(timestamp) || StrKit.isBlank(nonce)) { controller.renderText("check signature failure"); return false; } if (SignatureCheckKit.me.checkSignature(signature, timestamp, nonce)) { return true; } else { log.error("check signature failure: " + " signature = " + controller.getPara("signature") + " timestamp = " + controller.getPara("timestamp") + " nonce = " + controller.getPara("nonce")); return false; } }
怎么样,释怀了吧?这就是检测的全过程,这里只是列出了一点点东西,若是你有兴趣,先到这里玩一下,你就知道我刚刚说的是什么了?
好了,这个第一步关于微信的检查 我就说到这里。不懂的留言吧。明天继续挖坟,绝对让你通透!!若是以为好,赏个赞,留个言鼓励下吧!!挖坟其实能挖到很多好东西了!