1.微信验证接口

package com.park.utils.wechatUtil;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; @RestController
@RequestMapping(value = "/wechat")
public class tokenVerify { @RequestMapping(value = "/tokenVerify",method = RequestMethod.GET)
public String tokenVerify(HttpServletRequest request){
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr"); Boolean isVerify = SignUtil.checkSignature(signature,timestamp,nonce); if(isVerify){
return echostr;
}else {
return "VerifyFail";
} } }

2.判断工具类

package com.park.utils.wechatUtil;

import java.security.MessageDigest;
import java.util.Arrays; public class SignUtil {
private static String token = "weixin"; public static boolean checkSignature(String signature, String timestamp, String nonce) {
boolean result = false; // 对token、timestamp和nonce按字典序排序
String[] array = new String[]{token, timestamp, nonce};
Arrays.sort(array); // 将三个参数字符拼接成一个字符串
String str = array[0].concat(array[1]).concat(array[2]); String sha1Str = null;
try {
// 对拼接后的字符串进行sha1加密
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] digest = md.digest(str.getBytes());
sha1Str = byte2str(digest);
}
catch(Exception e) {
} if(sha1Str != null && sha1Str.equals(signature)) {
result = true;
} return result;
} /*
* 将字节数组转换成字符串
*/
public static String byte2str(byte[] array) {
StringBuffer hexstr = new StringBuffer();
String shaHex="";
for(int i = 0; i < array.length; i++) {
shaHex = Integer.toHexString(array[i] & 0xFF);
if(shaHex.length() < 2) {
hexstr.append(0);
}
hexstr.append(shaHex);
}
return hexstr.toString();
}
}

java微信token校验的更多相关文章

  1. Java微信公众平台开发_02_启用服务器配置

    源码将在晚上上传到 github 一.准备阶段 需要准备事项: 1.一个能在公网上访问的项目: 见:[  Java微信公众平台开发_01_本地服务器映射外网  ] 2.一个微信公众平台账号: 去注册: ...

  2. java微信开发API解析(二)-获取消息和回复消息

    java微信开发API解析(二)-获取消息和回复消息 说明 * 本演示样例依据微信开发文档:http://mp.weixin.qq.com/wiki/home/index.html最新版(4/3/20 ...

  3. Java微信二次开发(一)

    准备用Java做一个微信二次开发项目,把流程写在这里吧. 第一天,做微信请求验证 需要导入库:servlet-api.jar 第一步:新建包com.wtz.service,新建类LoginServle ...

  4. Java微信公众平台开发_03_消息管理之被动回复消息

    GitHub源码:https://github.com/shirayner/weixin_gz 一.本节要点 1.回调url 上一节,我们启用服务器配置的时候,填写了一个服务器地址(url),如下图, ...

  5. Java微信公众号开发梳理

    Java微信公众号开发梳理 现在微信公众平台的开发已经越来越普遍,这次开发需要用到微信公众平台.因此做一个简单的记录,也算是给那些没踩过坑的童鞋一些启示吧.我将分几块来简单的描述一下,之后会做详细的说 ...

  6. 微信token验证失败的解决方法

    一.问题由来 在使用URL和Token启用微信公众平台开发模式消息接口的时候,我们会碰到下面三种情况 1. token校验失败 这样回头检查一下各项配置是否正确.如果确定配置没有问题,请按下面的方法检 ...

  7. java微信开发框架wechat4j入门教程

    wechat4j What is wechat4j? wechat develop framework for java(微信开发框架JAVA版,最简单易用微信开发框架) wechat4j可以用来干什 ...

  8. java微信接口之五—消息分组群发

    一.微信消息分组群发接口简介 1.请求:该请求是使用post提交地址为: https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_t ...

  9. java微信接口之四—上传素材

    一.微信上传素材接口简介 1.请求:该请求是使用post提交地址为: https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=A ...

随机推荐

  1. Django-常用异常

    1 from rest_framework.authentication import BasicAuthentication raise AuthenticationFailed(res.dict) ...

  2. python实现随机生成头像

    今天遇到如何给用户分配随机头像的问题,想着要在本地放很多图片,有点无聊,就找了一些生成头像的工具.发现gravatar生成图像还不错,挺好玩的. 1.下面上代码 # -*- coding: utf-8 ...

  3. 区间DP小结 及例题分析:P1880 [NOI1995]石子合并,P1063 能量项链

    区间类动态规划 一.基本概念 区间类动态规划是线性动态规划的拓展,它在分阶段划分问题时,与阶段中元素出现的顺序和由前一阶段的那些元素合并而来由很大的关系.例如状态f [ i ][ j ],它表示以已合 ...

  4. 爬虫之解析库Xpath

    简介 XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言. XPath基于XML的树状结构,提供在数据结构树中找寻节点的能力.起初XPat ...

  5. Js 之将html转为图片html2canvas

    一.效果图 二.代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  6. NMS(非极大值抑制)实现

    1.IOU计算 设两个边界框分别为A,B.A的坐标为Ax1,Ax2,Ay1,Ay2,且Ax1 < Ax2,Ay1 < Ay2.B和A类似. 则IOU为A∩B除以A∪B. 当两个边界框有重叠 ...

  7. 20182332 实验一《Linux基础与Java开发环境》实验报告

    20182332 实验一<Linux基础与Java开发环境>实验报告 课程:<程序设计与数据结构> 班级: 1823 姓名: 盛国榕 学号:20182332 实验教师:王志强 ...

  8. 求N个集合的并集

    做容器放置实验时,需要计算下载N个Images的总size(Image之间可能会有可以共享的size). 一开始想到的是Images两两之间求交集,然后慢慢推到了容斥原理...时间复杂度大概就是O(N ...

  9. Linux -bash: redis-cli: command not found(亲测可行)

    Linux 安装完redis单独用命令: ? 1 redis-server 报错: ? 1 -bash: redis-server: command not found 说明redis-server不 ...

  10. uwsgi+nginx部署django项目

    1. 概念解析(wsgi协议,uwsgi协议,uWSGI) 参考:https://www.cnblogs.com/wspblog/p/8575101.html 1.1 现实世界的web请求: 1.2  ...