您的位置:首页 > 其它

公众服务号开启回调模式与企业号开始回调的区别

2016-06-05 06:55 387 查看
我以前做过微信企业开发,后来去做了微信公众号开发,回调模式 应该是很简单的,可我 没认真看文档,以为与企业号一样,整整坑了我 1天时间 。

代码如下:

完整代码下载地址

http://note.youdao.com/yws/public/redirect/share?id=06bc08978584b66e3aba953375ecd912&type=false

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Logger;

import com.qq.weixin.mp.aes.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qq.weixin.mp.aes.WXBizMsgCrypt;

public class Test extends HttpServlet {
WXBizMsgCrypt wxcpt;
private Logger log = Logger.getLogger(this.getClass().getName());
public static String appid = "XXX";
public static String sToken = "weixin";
public static String sEncodingAESKey = "XXXwwwwwwwwwwwwwwwwwwwwwwww";

public static void main(String[] args) throws Exception {

//testForCheckMsgCrypt();

testForCheckSignature();
}

public static void testForCheckSignature() throws Exception{

String signature="7c1bb93774b9f5810e6f3d7bf0a5b730735581a9";

String echostr="1467575823083721407";

String timestamp="1465077934";

String nonce="512769189";

String csignature=CheckSignature(sToken, timestamp, nonce);

System.out.println("csignature:"+csignature);
System.out.println("csignature:"+csignature);

}

public static void testForCheckMsgCrypt() throws Exception{

WXBizMsgCrypt wxcpt;

wxcpt = new WXBizMsgCrypt(sToken, sEncodingAESKey, appid);

testIllegalAesKey(sToken,sEncodingAESKey,appid);

String signature="7c1bb93774b9f5810e6f3d7bf0a5b730735581a9";

String echostr="1467575823083721407";

String timestamp="1465077934";

String nonce="512769189";

System.out.println(echostr+":"+URLDecoder(echostr));

String sEchoStr=wxcpt.verifyUrl(signature, timestamp, nonce, echostr);

// 验证URL成功,将sEchoStr返回

System.out.println("sEchoStr:"+sEchoStr);
}

/**
* 确认请求来自微信服务器
* @throws IOException
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{

checkSignForService(request,response);

}
//服务号的校验
public void checkSignForService(HttpServletRequest request, HttpServletResponse response) throws IOException{

// 微信加密签名
String signature = request.getParameter("signature");
// 随机字符串
String echostr = request.getParameter("echostr");
// 时间戳
String timestamp = request.getParameter("timestamp");
// 随机数
String nonce = request.getParameter("nonce");

String csignature=CheckSignature(sToken, timestamp, nonce);

PrintWriter out = response.getWriter();

if(csignature.equals(signature)){
out.println(echostr);
}
out.close();

}
//企业号的校验
public void checkSignForEnterprise(HttpServletRequest request, HttpServletResponse response) throws IOException{

response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");

/////////////////////////////////////

System.out.println("#############################");
Enumeration enu=request.getParameterNames();
while(enu.hasMoreElements()){
String paraName=(String)enu.nextElement();
System.out.println(paraName+": "+URLDecoder(request.getParameter(paraName)));
}
System.out.println("#############################");

/////////////////////////////////////
// 微信加密签名
String signature = request.getParameter("signature");
// 随机字符串
String echostr = request.getParameter("echostr");
// 时间戳
String timestamp = request.getParameter("timestamp");
// 随机数
String nonce = request.getParameter("nonce");

String sEchoStr; //需要返回的明文
PrintWriter out = response.getWriter();

try {
wxcpt = new WXBizMsgCrypt(sToken, sEncodingAESKey, appid);

testIllegalAesKey(sToken,sEncodingAESKey,appid);
sEchoStr=wxcpt.verifyUrl(signature, timestamp, nonce, echostr);
// 验证URL成功,将sEchoStr返回
System.out.println("sEchoStr:"+sEchoStr);
out.print(sEchoStr);
} catch (Exception e1) {
e1.printStackTrace();
}

out.print(signature);
}

/**
* 处理微信服务器发来的消息
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

Enumeration enu=request.getParameterNames();
while(enu.hasMoreElements()){
String paraName=(String)enu.nextElement();
System.out.println(paraName+": "+request.getParameter(paraName));
}

}

public static String URLDecoder(String para){
String result="";
try {
result=URLDecoder.decode(para,"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;

}

public static void testIllegalAesKey(String token,String sEncodingAESKey,String appId) {
try {
new WXBizMsgCrypt(token, sEncodingAESKey, appId);
System.out.println("success!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("fail!");
return;
}
}
//自己写的签名方法
public static String CheckSignature(String token,String timestamp,String nonce){
List<String> params = new ArrayList<String>();
params.add(token);
params.add(timestamp);
params.add(nonce);
// 1. 将token、timestamp、nonce三个参数进行字典序排序
Collections.sort(params, new Comparator<String>() {
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
// 2. 将三个参数字符串拼接成一个字符串进行sha1加密
String temp = SHA1.encode(params.get(0) + params.get(1) + params.get(2));

return temp;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: