微信开发(一)搭建开发环境
说明
正文
配置本地服务器
在下方的客户端下载中选择适合自己电脑的版本
在申请时注意隧道协议为web,且本地端口为80 申请好会为隧道分配一个authtoken,这个token时让NATAPP可以运行起来的关键,接下来在运行前必须配置文件,关于配置文件的下载地址: ,更多教程参考: 配置文件必须与之前解压的natapp.exe在同一级目录,然后将得到的authtoken填入保存。
运行结果为: 这里可以看到随机分配的域名指向了本地的80端口。这里的域名就是测试号配置URL需要填写的参数。
关于本地服务器的搭建到这里就结束了,接下来在之前申请的测试号中进行配置。
配置测试号
URL即为NATAPP运行时得到的域名,Token随意填写,程序中需要使用。
校验代码:
@RequestMapping(value="hello", method = RequestMethod.GET) public void hello(HttpServletRequest request, HttpServletResponse response){ System.out.println("success"); String signature = request.getParameter("signature"); String timestamp = request.getParameter("timestamp"); String nonce = request.getParameter("nonce"); String echostr = request.getParameter("echostr"); PrintWriter out = null; try { out = response.getWriter(); if(CheckUtil.checkSignature(signature,timestamp, nonce)){ out.write(echostr); } } catch (IOException e) { e.printStackTrace(); } finally { out.close(); } }
import java.security.MessageDigest; import java.util.Arrays; public class CheckUtil { public static final String tooken = "testdemo"; public static boolean checkSignature(String singnature, String timestamp, String nonce){ String[] arr = {tooken, timestamp,nonce}; Arrays.sort(arr); StringBuilder sb = new StringBuilder(); for(String s : arr){ sb.append(s); } String temp = getSha1(sb.toString()); return temp.equals(singnature); } private static String getSha1(String str){ if(str==null||str.length()==0){ return null; } char hexDigits[] = { 0,1,2,3,4,5,6,7,8,9, a,b,c,d,e,f}; try { MessageDigest mdTemp = MessageDigest.getInstance("SHA1"); mdTemp.update(str.getBytes("UTF-8")); byte[] md = mdTemp.digest(); int j = md.length; char buf[] = new char[j*2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; buf[k++] = hexDigits[byte0 >>> 4 & 0xf]; buf[k++] = hexDigits[byte0 & 0xf]; } return new String(buf); } catch (Exception e) { return null; } } }
能正常响应时,就会出现配置成功,接下来就可以进行验证开发了。
上一篇:
uniapp开发微信小程序-2.页面制作
下一篇:
thinkphp全开源社区团购接龙群接龙