微信开发者验证接口开发
编程技术  /  houtizong 发布于 3年前   161
/* * Copyright (c) 2014-2018 Swingsoft Co.Ltd. All rights reserved. */package com.mn606.weixin.action;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.mn606.weixin.util.AccessUtil;/** * 微信开发者验证接口 * * @author Swing */public class AccessAction extends HttpServlet {/** * Comment for <code>serialVersionUID</code> */private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String signature = req.getParameter("signature");String timestamp = req.getParameter("timestamp");String nonce = req.getParameter("nonce");String echostr = req.getParameter("echostr");if (AccessUtil.checkSignature(signature, timestamp, nonce)) {resp.getWriter().write(echostr);resp.getWriter().flush();resp.getWriter().close();}}}
/* * Copyright (c) 2014-2018 Swingsoft Co.Ltd. All rights reserved. */package com.mn606.weixin.util;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Arrays;/** * 微信开发者工具类 * * @author Swing */public class AccessUtil {public final static String TOKEN = "abcdefg";/** * * 校验签名是否正确 * * @param signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。 * @param timestamp 时间戳 * @param nonce 随机数 * @return 正确返回true */public static boolean checkSignature(String signature, String timestamp, String nonce) {// 验证参数是否为空if (isBank(signature) || isBank(timestamp) || isBank(nonce)) {return false;}// 1.将token、timestamp、nonce三个参数进行字典序排序String tmpStr = sort(TOKEN, timestamp, nonce);System.out.println(tmpStr);// 2.将三个参数字符串拼接成一个字符串进行sha1加密tmpStr = sha1(tmpStr);System.out.println(tmpStr);// 3.开发者获得加密后的字符串可与signature对比,标识该请求来源于微信if (tmpStr.equals(signature)) {return true;} else {return false;}}/** * * 进行字典序排序 * * @param value * @return */public static String sort(String token, String timestamp, String nonce) {StringBuilder result = new StringBuilder();String[] strArr = { TOKEN, timestamp, nonce };Arrays.sort(strArr);for (int i = 0; i < strArr.length; i++) {result.append(strArr[i]);}return result.toString();}/** * * 判断参数是否为空 * * @param value * @return */public static boolean isBank(String value) {if (value != null && !value.trim().isEmpty()) {return false;}return false;}/** * * 使用SHA1加密 * * @param input * @return * @throws NoSuchAlgorithmException */public static String sha1(String input) {MessageDigest mDigest;try {mDigest = MessageDigest.getInstance("SHA1");byte[] result = mDigest.digest(input.getBytes());StringBuffer sb = new StringBuffer();for (int i = 0; i < result.length; i++) {sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));}return sb.toString();} catch (NoSuchAlgorithmException e) {return null;}}}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接