|
测试运行不能显示“非法请求”
报错如下:
[Fatal Error] :1:1: 文件提前结束。
六月 12, 2015 4:11:52 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet wxproservlet threw exception
java.lang.RuntimeException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 文件提前结束。
at me.chanjar.weixin.common.util.crypto.WxCryptUtil.extractEncryptPart(WxCryptUtil.java:299)
at me.chanjar.weixin.common.util.crypto.WxCryptUtil.decrypt(WxCryptUtil.java:160)
at me.chanjar.weixin.cp.bean.WxCpXmlMessage.fromEncryptedXml(WxCpXmlMessage.java:423)
at me.chanjar.weixin.cp.bean.WxCpXmlMessage.fromEncryptedXml(WxCpXmlMessage.java:432)
at wxpro.wxproservlet.service(wxproservlet.java:90)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 文件提前结束。
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
at me.chanjar.weixin.common.util.crypto.WxCryptUtil.extractEncryptPart(WxCryptUtil.java:294)
... 17 more
程序的weixin-java-tools 是1.0.6
程序源代码:
package wxpro;
import java.util.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import me.chanjar.weixin.common.util.StringUtils;
//import me.chanjar.weixin.cp.api.WxCpConfigStorage;
import me.chanjar.weixin.cp.api.WxCpInMemoryConfigStorage;
import me.chanjar.weixin.cp.api.WxCpMessageHandler;
import me.chanjar.weixin.cp.api.WxCpMessageRouter;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage;
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil;
/**
* Servlet implementation class wxproservlet
*/
public class wxproservlet extends HttpServlet {
private static final long serialVersionUID = 10L;
protected WxCpInMemoryConfigStorage wxCpConfigStorage;
protected WxCpService wxCpService;
protected WxCpMessageRouter wxCpMessageRouter;
@Override public void init() throws ServletException {
super.init();
wxCpConfigStorage = new WxCpInMemoryConfigStorage();
wxCpConfigStorage.setCorpId("wx5f2b66dea8f30a5c"); // 设置微信企业号的appid
wxCpConfigStorage.setCorpSecret("xQ0MClBI9idRRUBj9y-AXToR1L77GqcqbYGkqOiqLQ_468QOxLU-M9D98KuhFXtX"); // 设置微信企业号的app corpSecret
wxCpConfigStorage.setAgentId("0"); // 设置微信企业号应用ID
wxCpConfigStorage.setToken("qwOfmKkRjXX2pjMezZSuFgpoDVbcPf"); // 设置微信企业号应用的token
//wxCpConfigStorage.setAesKey("YPstbnxqJaRcDZbRPwE7xRPIzzECYIir3GsA1X5BniB"); // 设置微信企业号应用的EncodingAESKey
wxCpService = new WxCpServiceImpl();
wxCpService.setWxCpConfigStorage(wxCpConfigStorage);
WxCpMessageHandler handler = new WxCpMessageHandler() {
@Override public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService) {
WxCpXmlOutTextMessage m = WxCpXmlOutMessage
.TEXT()
.content("测试加密消息")
.fromUser(wxMessage.getToUserName())
.toUser(wxMessage.getFromUserName())
.build();
return m;
}
};
wxCpMessageRouter = new WxCpMessageRouter(wxCpService);
wxCpMessageRouter
.rule()
.async(false)
.content("哈哈") // 拦截内容为“哈哈”的消息
.handler(handler)
.end();
}
@Override protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
String msgSignature = request.getParameter("msg_signature");
String nonce = request.getParameter("nonce");
String timestamp = request.getParameter("timestamp");
String echostr = request.getParameter("echostr");
if (StringUtils.isNotBlank(echostr)) {
if (!wxCpService.checkSignature(msgSignature, timestamp, nonce, echostr)) {
// 消息签名不正确,说明不是公众平台发过来的消息
response.getWriter().println("非法请求");
return;
}
WxCpCryptUtil cryptUtil = new WxCpCryptUtil(wxCpConfigStorage);
String plainText = cryptUtil.decrypt(echostr);
// 说明是一个仅仅用来验证的请求,回显echostr
response.getWriter().println(plainText);
return;
}
WxCpXmlMessage inMessage = WxCpXmlMessage.fromEncryptedXml(request.getInputStream(), wxCpConfigStorage, timestamp, nonce, msgSignature);
WxCpXmlOutMessage outMessage = wxCpMessageRouter.route(inMessage);
if (outMessage != null) {
response.getWriter().write(outMessage.toEncryptedXml(wxCpConfigStorage));
}
return;
}
}
|
|