1、参数获取,登录服务号
2、更改参数
package com.kp.utils;
public interface Constants {
String APPID = "";
String TOKEN = "";
String SECRET = "";
String encodingAESKey = "";
3、url 配置可以参考企业号开发
http://blog.csdn.net/u014520797/article/details/49720601
花生壳映射
http://blog.csdn.net/u014520797/article/details/49667217
4、主要代码
package com.kp.controller;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.kp.aes.AesException;
import com.kp.aes.WXBizMsgCrypt;
import com.kp.service.CoreService;
import com.kp.utils.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.apache.commons.io.IOUtils;
/**
* 注解方式打开链接
* @author py
*/
@Controller
public class CoreController {
private String token = Constants.TOKEN;
private String encodingAESKey =Constants.encodingAESKey;
private String appID = Constants.APPID;
@RequestMapping(value = { "/wxJoin.do" }, method = RequestMethod.GET)
public void coreJoinGet(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// 微信加密签名(企业号为msg_signature)
// String msg_signature = request.getParameter("msg_signature");
//与企业号不同1
String signature = request.getParameter("signature");
// 时间戳
String timestamp = request.getParameter("timestamp");
// 随机数
String nonce = request.getParameter("nonce");
// 随机字符串
String echostr = request.getParameter("echostr");
System.out.println("signature:"+signature);
System.out.println("timestamp:"+timestamp);
System.out.println("nonce:"+nonce);
System.out.println("echostr:"+echostr);
PrintWriter out = response.getWriter();
// 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
String result = null;
try {
WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAESKey,
appID);
result = wxcpt.VerifyURL(signature, timestamp, nonce, echostr);
System.out.println("result:"+result);
} catch (AesException e) {
e.printStackTrace();
}
if (result == null) {
result = token;
}
out.print(result);
out.close();
out = null;
}
@RequestMapping(value = { "/wxJoin.do" }, method = RequestMethod.POST)
public void coreJoinPost(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// 将请求、响应的编码均设置为UTF-8(防止中文乱码)
System.out.println("********进来了 coreJoin.do POST");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
// 微信加密签名
String msg_signature = request.getParameter("signature");
// 时间戳
String timestamp = request.getParameter("timestamp");
// 随机数
String nonce = request.getParameter("nonce");
//从请求中读取整个post数据
InputStream inputStream = request.getInputStream();
String postData = IOUtils.toString(inputStream, "UTF-8");
System.out.println("postData:"+postData);
String msg = "";
WXBizMsgCrypt wxcpt = null;
try {
wxcpt = new WXBizMsgCrypt(token, encodingAESKey, appID);
//解密消息
msg = wxcpt.DecryptMsg(msg_signature, timestamp, nonce, postData);
} catch (AesException e) {
e.printStackTrace();
}
System.out.println("msg=" + msg);
// 调用核心业务类接收消息、处理消息
String respMessage = CoreService.processRequest(msg);
System.out.println("respMessage=" + respMessage);
String encryptMsg = "";
try {
//加密回复消息
encryptMsg = wxcpt.EncryptMsg(respMessage, timestamp, nonce);
System.out.println("encryptMsg:"+encryptMsg);
} catch (AesException e) {
e.printStackTrace();
}
// 响应消息
PrintWriter out = response.getWriter();
out.print(encryptMsg);
out.close();
}
}
5、如果采用上面的代码,那么url则是 http://域名/WXFWDemo/wxJoin.do。
注意:url不能为ip地址拼接
6、源码地址:http://download.csdn.net/detail/u014520797/9714496,
已经测试过可以配置成功。java编写,代码采用springmvc框架。