微信公众号开发笔记-验证token
<h2 class="md-end-block md-heading"><span class="md-plain">开发</span></h2><div class="md-hr md-end-block"><hr></div>
<p class="md-end-block md-p">话不多说我们直接进入主题</p>
<p class="md-end-block md-p"> </p>
<p class="md-end-block md-p">我们先去微信公众号申请一个公众号:<br></p>
<p class="md-end-block md-p">申请完成之后我们找到开发下的基本配置</p>
<p class="md-end-block md-p"><img src="https://img2018.cnblogs.com/blog/1810507/201910/1810507-20191015085036325-1550394405.png" alt="" width="516" height="343"></p>
<p class="md-end-block md-p">然后找到进行基本配置,我们需要一个url地址来验证,这里的地址必需要是外网,Token是我们任意填写然后在程序后端填写一致的token进行验证</p>
<p class="md-end-block md-p">这里的外网地址也就是域名我们一般的同学肯定是没有的辣,所以我们使用一个叫内网穿透的东东,我们先把网页停留在这个页面</p>
<p> </p>
<p> </p>
<p> <img src="https://img2018.cnblogs.com/blog/1810507/201910/1810507-20191015085156891-1278581053.png" alt="" width="531" height="256"></p>
<p> </p>
<p> </p>
<p class="md-end-block md-p"><span class="md-plain">我们利用natapp实现内网穿透(个人推荐natapp<span class="md-softbreak"> </span>)</span></p>
<p class="md-end-block md-p"><span class="md-plain">百度natapp:https://natapp.cn/注册一个账号下载natapp ,然后点击购买隧道有免费的</span></p>
<p class="md-end-block md-p"><img src="https://img2018.cnblogs.com/blog/1810507/201910/1810507-20191015085454659-1008950049.png" alt="" width="656" height="283"></p>
<p> </p>
<p>然后我们点击配置:</p>
<p><img src="https://img2018.cnblogs.com/blog/1810507/201910/1810507-20191015085701214-1276714777.png" alt="" width="806" height="581"></p>
<p> </p>
<p> 绑定域名我们自己任意填写,地址端口如图(tomcat端口是多少就是多少,地址为127.0.0.1)</p>
<p> <img src="https://img2018.cnblogs.com/blog/1810507/201910/1810507-20191015085846213-1877511327.png" alt="" width="798" height="206"></p>
<p> </p>
<p> 接着在我的隧道里面找到隧道复制authtoken。也就是上面我末尾为e283的,你的只有你能看到</p>
<p class="md-end-block md-p">通过在DOS窗口运行natapp -authtoken *******(*******代表你在natapp官网他给你的authtoken)指令得到如下图,启动tomcat之后此时我们就可以通过tree.natapp1.cc访问tomcat80端口了</p>
<p class="md-end-block md-p"><span class="md-plain"><span class="md-softbreak"><img src="https://img2018.cnblogs.com/blog/1810507/201910/1810507-20191011155725812-2145869920.png" alt="" width="678" height="413"></span></span></p>
<p> </p>
<p> 注意:这里我域名为tree.natapp1.cc,小伙伴们需要根据自己设置的域名来访问</p>
<p><img src="https://img2018.cnblogs.com/blog/1810507/201910/1810507-20191011160203221-1888743751.png" alt="" width="683" height="339"></p>
<p> </p>
<p> 我们启动tomcat后能通过自己设置的域名(我的为tree.natapp1.cc)访问到tomcat的8080端口就说明成功了!</p>
<p class="md-end-block md-p"><span class="md-plain"><span class="md-softbreak"><span class="md-plain">好了,我们开始创建项目;</span></span></span></p>
<p class="md-end-block md-p"><span class="md-plain"><span class="md-softbreak"><span class="md-plain">注意:Wechat<span class="md-softbreak"> <span class="md-plain">本次需要的jar包有servlet-api.jar(因为我自己开发的过程中没有这个包是后面导入的所以提醒一下)</span></span></span></span></span></p>
<p class="md-end-block md-p md-focus"><span class="md-plain">首先创建weixinServlet文件(在创建时我们可以修改访问地址如图)</span></p>
<p><img src="https://img2018.cnblogs.com/blog/1810507/201910/1810507-20191011154302443-27172402.png" alt="" width="432" height="459"></p>
<p>我们将URL mapping中的内容选中,然后点击Edit,改成任意自己想访问的就行了。</p>
<p class="md-end-block md-p md-focus"><span class="md-plain"><span class="md-softbreak"><span class="md-plain">通过微信开发文档我们知道微信后台会通过GET方式发送signature,timestamp,nonce,echostr。</span></span></span></p>
<p class="md-end-block md-p md-focus"><span class="md-plain"><span class="md-softbreak"><span class="md-plain"><img src="https://img2018.cnblogs.com/i-beta/1810507/201912/1810507-20191207151834896-263570801.png" alt=""></span></span></span></p>
<p> </p>
<p> </p>
<p class="md-end-block md-p md-focus"><span class="md-plain"><span class="md-softbreak"><span class="md-plain">我们将token,timestamp,nonce进行字典序排序sha1加密后与signature进行对比。</span></span></span></p>
<p class="md-end-block md-p md-focus"><span class="md-plain"><span class="md-softbreak"><span class="md-plain"><span class="md-softbreak"><span class="md-plain">通过则可以进行开发,其中涉及到的sha1加密算法及weixinServlet代码如下</span></span></span></span></span></p>
<p class="md-end-block md-p md-focus"> 这些可以在罗召勇老师的视频中有详细的讲解,链接我放在下面。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> servlet;
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.IOException;
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.PrintWriter;
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.servlet.ServletException;
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.servlet.ServletInputStream;
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.servlet.ServletOutputStream;
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.servlet.annotation.WebServlet;
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.servlet.http.HttpServlet;
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.servlet.http.HttpServletRequest;
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.servlet.http.HttpServletResponse;
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> service.WxService;
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 128, 0, 1)">/**</span>
<span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 128, 0, 1)"> * Servlet implementation class weixinServlet
</span><span style="color: rgba(0, 128, 128, 1)">16</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">17</span> @WebServlet("/wx"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> weixinServlet <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> HttpServlet {
</span><span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">long</span> serialVersionUID = 1L<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">20</span>
<span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 128, 0, 1)">/**</span>
<span style="color: rgba(0, 128, 128, 1)">22</span> <span style="color: rgba(0, 128, 0, 1)"> * Default constructor.
</span><span style="color: rgba(0, 128, 128, 1)">23</span> <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">24</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> weixinServlet() {
</span><span style="color: rgba(0, 128, 128, 1)">25</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated constructor stub</span>
<span style="color: rgba(0, 128, 128, 1)">26</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">27</span> <span style="color: rgba(0, 128, 0, 1)">/**</span>
<span style="color: rgba(0, 128, 128, 1)">28</span> <span style="color: rgba(0, 128, 0, 1)"> * </span><span style="color: rgba(128, 128, 128, 1)">@see</span><span style="color: rgba(0, 128, 0, 1)"> HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
</span><span style="color: rgba(0, 128, 128, 1)">29</span> <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">30</span> <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span> doGet(HttpServletRequest request, HttpServletResponse response) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> ServletException, IOException {
</span><span style="color: rgba(0, 128, 128, 1)">31</span>
<span style="color: rgba(0, 128, 128, 1)">32</span> String signature=request.getParameter("signature"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">33</span> String timestamp=request.getParameter("timestamp"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">34</span> String nonce=request.getParameter("nonce"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">35</span> String echostr=request.getParameter("echostr"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">36</span>
<span style="color: rgba(0, 128, 128, 1)">37</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(WxService.check(timestamp,nonce,signature)) {
</span><span style="color: rgba(0, 128, 128, 1)">38</span> System.out.println("接入成功"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">39</span> PrintWriter out=<span style="color: rgba(0, 0, 0, 1)">response.getWriter();
</span><span style="color: rgba(0, 128, 128, 1)">40</span> <span style="color: rgba(0, 0, 0, 1)"> out.print(echostr);
</span><span style="color: rgba(0, 128, 128, 1)">41</span> <span style="color: rgba(0, 0, 0, 1)"> out.flush();
</span><span style="color: rgba(0, 128, 128, 1)">42</span> <span style="color: rgba(0, 0, 0, 1)"> out.close();
</span><span style="color: rgba(0, 128, 128, 1)">43</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">44</span> <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">45</span> System.out.println("失败"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">46</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">47</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">48</span>
<span style="color: rgba(0, 128, 128, 1)">49</span> <span style="color: rgba(0, 128, 0, 1)">/**</span>
<span style="color: rgba(0, 128, 128, 1)">50</span> <span style="color: rgba(0, 128, 0, 1)"> * </span><span style="color: rgba(128, 128, 128, 1)">@see</span><span style="color: rgba(0, 128, 0, 1)"> HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
</span><span style="color: rgba(0, 128, 128, 1)">51</span> <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">52</span> <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span> doPost(HttpServletRequest request, HttpServletResponse response) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> ServletException, IOException {
</span><span style="color: rgba(0, 128, 128, 1)">53</span> ServletInputStream is=<span style="color: rgba(0, 0, 0, 1)">request.getInputStream();
</span><span style="color: rgba(0, 128, 128, 1)">54</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[] b=<span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>;
</span><span style="color: rgba(0, 128, 128, 1)">55</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> len;
</span><span style="color: rgba(0, 128, 128, 1)">56</span> StringBuilder sb=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringBuilder();
</span><span style="color: rgba(0, 128, 128, 1)">57</span> <span style="color: rgba(0, 0, 255, 1)">while</span>((len=is.read(b))!=-1<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">58</span> sb.append(<span style="color: rgba(0, 0, 255, 1)">new</span> String(b,0<span style="color: rgba(0, 0, 0, 1)">,len));
</span><span style="color: rgba(0, 128, 128, 1)">59</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">60</span> <span style="color: rgba(0, 0, 0, 1)"> System.out.println(sb.toString());
</span><span style="color: rgba(0, 128, 128, 1)">61</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">62</span> }</pre>
</div>
<p>WxService.java如下</p>
<div class="cnblogs_Highlighter">
<pre class="brush:java;gutter:true;">package service;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class WxService {
private static final String TOKEN="bestsegho0914";
public static boolean check(String timestamp,String nonce,String signature) {
String[] strs=new String[] {TOKEN,timestamp,nonce};
Arrays.sort(strs);
String str=strs+strs+strs;
String mysig=sha1(str);
System.out.println(mysig);
System.out.println(signature);
return mysig.equals(signature);
}
private static String sha1(String src) {
try {
MessageDigest md=MessageDigest.getInstance("sha1");
byte[] digest=md.digest(src.getBytes());
char[] chars= {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
StringBuilder sb=new StringBuilder();
for (byte b:digest) {
sb.append(chars[(b>>4)&15]);
sb.append(chars);
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}</pre>
</div>
<p>完成代码后我们点击运行 run as ->run on server,在tomcat上启动</p>
<p>然后回到我们微信公众号的后台,点击配置信息的修改按钮 点击提交 这里就会显示配置成功</p>
<p><img src="https://img2018.cnblogs.com/blog/1810507/201910/1810507-20191015084603618-1279854303.png" alt="" width="713" height="359"></p>
<p> </p>
<p> </p>
<p> 配置成功如图</p>
<p><img src="https://img2018.cnblogs.com/blog/1810507/201910/1810507-20191015084400129-1257128826.png" alt="" width="729" height="306"></p>
<p> </p>
<p> </p>
<p> 这时我们就可以开始进行公众号个人开发了。</p>
<p>本人也是初次进行微信的java开发,分享一些我正在看的资料希望能帮到想要学习微信开发的小伙伴们</p>
<p>罗召勇java开发:https://www.bilibili.com/video/av35042298</p>
<p>微信开发帮助文档:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html</p>
<p>微信公众号测试号申请入口:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login</p>
<p>ps:所有文章用于个人学习,仅代表个人理解。</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/segho/p/11654719.html
頁:
[1]