作者:爱的甜蜜日记2010 | 来源:互联网 | 2023-09-11 10:54
一、注册公众号并认证二、准备一台服务器,必须用80端口三、定义一个接口,用于与微信服务器通信,配置开发者中心的服务器配置中的url1、服务器url下一个token,是自己定义的,但是要在你定义的这个接
一、注册公众号并认证
二、准备一台服务器,必须用80端口
三、定义一个接口,用于与微信服务器通信,配置开发者中心的服务器配置中的url
1、服务器url下一个token,是自己定义的,但是要在你定义的这个接口中也返回这个token,微信以此来验证这个token
2、微信服务器验证token时是使用get请求过来的,验证签名(接入指南中有参数、加密方式说明)后按要求返回
3、微信其他事件推送都是用该url,区别在于事件通知回调我们服务器都是post请求
4、微信公众平台中点提交,微信回调该url验证token,验证通过基本就ok了
四、上代码,我这里用springmvc写了个demo
package com.mx.controller;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mx.coder.CiphertextUtil;
import com.mx.util.MyException;
import com.mx.util.StaticMessage;
@Controller
public class WeixinController extends BaseController {
private Logger logger = LoggerFactory.getLogger(WeixinController.class);
/**
* 微信回调
* @author xiangpeng
* @date 2015-12-21上午9:54:57
* @param request
* @return
* String
*/
@RequestMapping(value = "weiXinCallBack", method = {RequestMethod.GET, RequestMethod.POST})
public @ResponseBody String wxCallBack(HttpServletRequest request){
//是否为get请求
boolean methodIsGet = request.getMethod().toLowerCase().equals("get");
try {
if (methodIsGet){
String signature = request.getParameter("signature");//微信加密签名
String timestamp = request.getParameter("timestamp");//时间戳
String nOnce= request.getParameter("nonce");//随机数
String echostr = request.getParameter("echostr");//随机字符串(需回传)
//请求参数非空判断
if (super.isEmpty(signature) || super.isEmpty(timestamp)
|| super.isEmpty(nonce) || super.isEmpty(echostr)){
logger.info(StaticMessage.PARAM_NOT_ENOUGH);
return StaticMessage.PARAM_NOT_ENOUGH;
}
logger.info("======微信请求参数======");
logger.info("signature="+signature);
logger.info("timestamp="+timestamp);
logger.info("nOnce="+nonce);
logger.info("echostr="+echostr);
//校验签名
boolean signBool = checkSign(signature, timestamp, nonce);
if (signBool){
return echostr;
}
}else{
}
} catch (Exception e) {
MyException.printExceptionInfo(logger, e);
}
return null;
}
/**
* 校验签名
* @author xiangpeng
* @date 2015-12-21上午10:59:26
* @param signature
* @param timestamp
* @param nonce
* @return
* boolean
*/
private boolean checkSign(String signature, String timestamp, String nonce) throws Exception{
//生成签名
List signList = new ArrayList();
signList.add(StaticMessage.TOKEN);
signList.add(timestamp);
signList.add(nonce);
//1. 将token、timestamp、nonce三个参数进行字典序排序
Collections.sort(signList, new Comparator() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
//2. 将三个参数字符串拼接成一个字符串进行sha1加密
Object[] signArray = signList.toArray();
StringBuffer signSb = new StringBuffer();
signSb.append(signArray[0]);
signSb.append(signArray[1]);
signSb.append(signArray[2]);
String signStr = signSb.toString();
signStr = CiphertextUtil.passAlgorithmsCiphering(signStr, CiphertextUtil.SHA_1);
logger.info("signStr="+signStr);
//3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
if (!signature.equals(signStr)){
logger.info("======校验签名失败!======");
logger.info("signature="+signature);
logger.info("signStr="+signStr);
return false;
}
return true;
}
}
package com.mx.coder;import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.ArrayList;import java.util.Collections;import java.util.List;import com.mx.util.StaticMessage;/** * MD5工具类 * * @date 2014-12-26 上午10:02:25 * * @author FCH * */public class CiphertextUtil {public static final String SHA_1 = "SHA-1";public static final String SHA_256 = "SHA-256";private static final char[] CH_HEX = { '0', '1', '2', '3', '4', '5', '6','7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };/** * 64位MD5 * * @param str * @return */public static String parseStrToMd5L64(String str) {String reStr = null;try {MessageDigest md5 = MessageDigest.getInstance("MD5");// reStr = base64en.encode(md5.digest(str.getBytes("UTF-8")));reStr = Base64Encoder.encode(md5.digest(str.getBytes("UTF-8")));} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}return reStr;}/** * 32位小写MD5 * * @param str * @return */public static String parseStrToMd5L32(String str) {String reStr = null;try {MessageDigest md5 = MessageDigest.getInstance("MD5");byte[] bytes = md5.digest(str.getBytes());StringBuffer stringBuffer = new StringBuffer();for (byte b : bytes) {int bt = b & 0xff;if (bt <16) {stringBuffer.append(0);}stringBuffer.append(Integer.toHexString(bt));}reStr = stringBuffer.toString();} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();}return reStr;}/** * 32位大写MD5 * * @param str * @return */public static String parseStrToMd5U32(String str) {String reStr = parseStrToMd5L32(str);if (reStr != null) {reStr = reStr.toUpperCase();}return reStr;}/** * 16位小写MD5 * * @param str * @return */public static String parseStrToMd5L16(String str) {String reStr = parseStrToMd5L32(str);if (reStr != null) {reStr = reStr.substring(8, 24);}return reStr;}/** * 16位大写MD5 * * @param str * @return */public static String parseStrToMd5U16(String str) {String reStr = parseStrToMd5U32(str);if (reStr != null) {reStr = reStr.substring(8, 24);}return reStr;} /** * 加密字符串 * * @param sourceStr 需要加密目标字符串 * @param algorithmsName 算法名称(如:MD5,SHA-1,SHA-256) * @return */ public static String passAlgorithmsCiphering(String sourceStr,String algorithmsName){ String password = ""; MessageDigest md; try { md = MessageDigest.getInstance(algorithmsName); // 使用指定byte[]更新摘要 md.update(sourceStr.getBytes()); // 完成计算,返回结果数组 byte[] b = md.digest(); password = byteArrayToHex(b); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return password; } /** * 将字节数组转为十六进制字符串 * * @param bytes * @return 返回16进制字符串 */ private static String byteArrayToHex(byte[] bytes) { // 一个字节占8位,一个十六进制字符占4位;十六进制字符数组的长度为字节数组长度的两倍 char[] chars = new char[bytes.length * 2]; int index = 0; for (byte b : bytes) { // 取字节的高4位 chars[index++] = CH_HEX[b >>> 4 & 0xf]; // 取字节的低4位 chars[index++] = CH_HEX[b & 0xf]; } return new String(chars); }public static void main(String[] args) {String str =passAlgorithmsCiphering("f", CiphertextUtil.SHA_1);System.out.println("加密后:" + str);}}