一般人默认是8080端口,必须要80端口和443端口。
微信需要一个请求一个URL来响应token验证,so !!! 也就是你点击确定的时候是向你指定的URL发送一个请求,在请求里面验证你的token是否正确。也就是我们需要一个外网接口让微信访问,这个接口不需要传入任何参数;注意必须是外网服务器接口,微信才能调用.
开始啦!在项目里边创建一个controller
@RequestMapping("/wechat")
@Controller
public class WechatController {
private static Logger logger = Logger.getLogger(WechatController.class);
private static String WECHAT_TOKEN = "jinsanpang";//这里的token填你在上边填写的。
@RequestMapping(value = "/wx.do")
public void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
logger.error("WechatController ---- WechatController");
System.out.println("========WechatController========= ");
logger.info("请求进来了...");
Enumeration pNames = request.getParameterNames();
while (pNames.hasMoreElements()) {
String name = (String) pNames.nextElement();
String value = request.getParameter(name);
// out.print(name + "=" + value);
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 (SignUtil.checkSignature(signature, timestamp, nonce)) {
out.print(echostr);
}
out.close();
out = null;
}
}
-------------------------------------------------------------------
再创建一个名为SignUtil.class
private static String token = "jinsanpang";
/**
* 校验签名
*/
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;
}
------------------------------------------------------
url填写你的服务器域名+wechat/wx.do token填jingsanpang 就可以了。