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. 「CF716D」Complete The Graph「最短路」

    题意 给定一个\(n\)个点\(m\)条边的无向图,有一些边权暂时为\(0\),你需要分配一个\([1, 10^{18}]\)的数.最终使得\(s\)到\(t\)最短路为\(L\),输出一个可行的分配 ...

  2. 串结构练习——字符串连接(SDUT 2124)

    Problem Description 给定两个字符串string1和string2,将字符串string2连接在string1的后面,并将连接后的字符串输出. 连接后字符串长度不超过110. Inp ...

  3. vue实战_从头开始搭建vue工程

    写在前面:vue工程入口文件分析 /index.html,/src/main.js,/src/APP.vue /index.html文件示例: <!DOCTYPE html> <ht ...

  4. win10windows无法创建快捷方式 请检查磁盘

      这个是因为文件没有权限造成的   打开"我的电脑",菜单栏里选择"工具"-"文件夹选项"-"查看",把里面" ...

  5. https://uwsgi-docs.readthedocs.io/en/latest/Async.html

    Beware! Async modes will not speed up your app, they are aimed at improving concurrency. Do not expe ...

  6. 回归regression

    X-Y存在某种映射关系,回归:确定出关系模型.

  7. Handler常见两种用法

    1.Handler在Android的两个功能 1.1表示未来某时做某事 1.2线程间通信 2.演示源码如下: package com.example.datastrorage; import andr ...

  8. linux上安装phpstudy

    摘要:安装:wget-chttp://lamp.phpstudy.net/phpstudy.bin chmod+xphpstudy.bin  #权限设置./phpstudy.bin#运行安装用时十到几 ...

  9. Linux Mint设置自定义快捷键

    我使用的是 Linux Mint 19.2 Tina 先搜索键盘,把键盘的功能调出来 快捷键--->>自定义快捷键--->>添加自定义快捷键 名称可自定义(这里我定义的是“截图 ...

  10. Windows10 64位安装DB2数据库

    安装前准备 : 系统:Windows10 64位 DB2 v9.5下载地址(迅雷):http://big3.ddooo.com/db2_93661.rar 选择安装包解压位置,并复制记住: 去到解压的 ...