最近在学习微信小程序,遇到的第一个问题就是需要配置服务器
关于这个服务器的配置我也是绕了好多弯路,说白了腾讯就是想通过你填的这个URL和Token去验证你有一个自己的服务器(外网可以访问的服务器),其实就是想让你证明你是你自己,呵呵....
关于这个token随便填不要纠结,下面直接看JAVA代码
package com.base.action; import java.io.PrintWriter; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping("/wechat") @Controller public class WechatController { private static Logger logger = Logger.getLogger(WechatController.class); private static String token = "xuejp"; @RequestMapping(value = "/wx.do") public void get(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("========WechatController========= "); logger.info("-----来自微信的请求----"); Enumeration pNames = request.getParameterNames(); while (pNames.hasMoreElements()) { String name = (String) pNames.nextElement(); String value = request.getParameter(name); //查看微信的请求都带了哪些参数 String log = "name =" + name + " value =" + value; logger.error(log); } String signature = request.getParameter("signature");/// 微信加密签名 String timestamp = request.getParameter("timestamp");/// 时间戳 String nonce = request.getParameter("nonce"); /// 随机数 String echostr = request.getParameter("echostr"); // 随机字符串 PrintWriter out = response.getWriter(); if (checkSignature(signature, timestamp, nonce)) { out.print(echostr); } out.print(token); out.close(); out = null; } /** * 校验签名 */ public static boolean checkSignature(String signature, String timestamp, String nonce) { System.out.println("signature:" + signature + "timestamp:" + timestamp + "nonc:" + nonce); String[] arr = new String[] { token, timestamp, nonce }; // 将token、timestamp、nonce三个参数进行字典序排序 Arrays.sort(arr); StringBuilder content = new StringBuilder(); for (int i = 0; i < arr.length; i++) { content.append(arr[i]); } MessageDigest md = null; String tmpStr = null; try { md = MessageDigest.getInstance("SHA-1"); // 将三个参数字符串拼接成一个字符串进行sha1加密 byte[] digest = md.digest(content.toString().getBytes()); tmpStr = byteToStr(digest); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } content = null; // 将sha1加密后的字符串可与signature对比,标识该请求来源于微信 System.out.println(tmpStr.equals(signature.toUpperCase())); return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false; } /** * 将字节数组转换为十六进制字符串 * * @param byteArray * @return */ private static String byteToStr(byte[] byteArray) { String strDigest = ""; for (int i = 0; i < byteArray.length; i++) { strDigest += byteToHexStr(byteArray[i]); } return strDigest; } /** * 将字节转换为十六进制字符串 * * @param mByte * @return */ private static String byteToHexStr(byte mByte) { char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; char[] tempArr = new char[2]; tempArr[0] = Digit[(mByte >>> 4) & 0X0F]; tempArr[1] = Digit[mByte & 0X0F]; String s = new String(tempArr); return s; } }
将以上代码部署到服务器上就可以了,在微信配置界面点击提交就会显示提交成功了,提交成功后一定要点击右上方的启用按钮
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。