微信开发者验证接口开发

微信开发者接口验证。
Token,自己随便定义,与微信填写一致就可以了。

根据微信接入指南描述 http://mp.weixin.qq.com/wiki/17/2d4265491f12608cd170a95559800f2d.html
第一步:填写服务器配置
第二步:验证服务器地址的有效性
第三步:依据接口文档实现业务逻辑

这里主要讲第二步验证服务器有效性。

建一个普通javaweb项目即可


建一个验证的Servlet,AccessAction.java

/*
 * 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;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected 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();
		}
	}
}




第二个创建验证工具类AccessUtil.java
/*
 * 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;
		}
	}

}




其他的servlet自己去配置吧。

完成后,自己去在微信的接口填写正确的url,token提交验证。

猜你喜欢

转载自362217990.iteye.com/blog/2226629