1、一個可以公網訪問的接入項目
2、需要一個企業微信賬號
企業微信官網:https://work.weixin.qq.com/
3、策略文件 報illegal key size異常時見
博客:http://www.cnblogs.com/shirui/p/7411735.html
4、微信的加密解密包
下載鏈接:http://qydev.weixin.qq.com/java.zip
5、企業微信api
鏈接:https://work.weixin.qq.com/api/doc
好了,上述是一些參考文件,以及遇到的一些bug的處理,接下就是開始配置接受消息的服務器配置了。
搭建環境:springmvc + maven + eclipse
首先看個流程
根據這個流程,來做具體操作,
1、建立controller
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.wechat.common.aes.AesException;
import com.wechat.common.aes.WXBizMsgCrypt;
import com.wechat.common.parameter.Parameter;
/**
* 應用模塊的controller
*
* @author WRY
*
*/
@Controller
public class ApplicationController {
@RequestMapping(value = "/ApplicationController")
public void EntryMethod(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 微信加密簽名
String msg_signature = request.getParameter("msg_signature");
// 時間戳
String timestamp = request.getParameter("timestamp");
// 隨機數
String nonce = request.getParameter("nonce");
// 隨機字符串
String echostr = request.getParameter("echostr");
System.out.println("request=" + request.getRequestURL());
PrintWriter out = response.getWriter();
// 通過檢驗msg_signature對請求進行校驗,若校驗成功則原樣返回echostr,表示接入成功,否則接入失敗
String result = null;
try {
WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(Parameter.TOKEN, Parameter.ENCODINDAESKEY, Parameter.CORPID);
result = wxcpt.VerifyURL(msg_signature, timestamp, nonce, echostr);
} catch (AesException e) {
e.printStackTrace();
}
if (result == null) {
result = Parameter.TOKEN;
}
out.print(result);
out.close();
out = null;
}
}
Parameter.TOKEN, Parameter.ENCODINDAESKEY,分別對應下面配置的token和encodingAESkey。 Parameter.CORPID 企業微信唯一的標識,在“我的企業”欄目下可查看
2.參數配置完成,點保存,如果成功則會提示設置成功,然後這個接口就可以正常開發邏輯了。