mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
整理及重构
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package me.chanjar.weixin.cp.demo;
|
||||
|
||||
import me.chanjar.weixin.cp.api.WxCpInMemoryConfigStorage;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@XmlRootElement(name = "xml")
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
class WxCpDemoInMemoryConfigStorage extends WxCpInMemoryConfigStorage {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleWxConfigProvider [appidOrCorpid=" + corpId + ", corpSecret=" + corpSecret + ", accessToken=" + accessToken
|
||||
+ ", expiresIn=" + expiresIn + ", token=" + token + ", aesKey=" + aesKey + "]";
|
||||
}
|
||||
|
||||
|
||||
public static WxCpDemoInMemoryConfigStorage fromXml(InputStream is) throws JAXBException {
|
||||
Unmarshaller um = JAXBContext.newInstance(WxCpDemoInMemoryConfigStorage.class).createUnmarshaller();
|
||||
InputSource inputSource = new InputSource(is);
|
||||
inputSource.setEncoding("utf-8");
|
||||
return (WxCpDemoInMemoryConfigStorage) um.unmarshal(inputSource);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package me.chanjar.weixin.cp.demo;
|
||||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.servlet.ServletHandler;
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class WxCpDemoServer {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Server server = new Server(8080);
|
||||
|
||||
ServletHandler handler = new ServletHandler();
|
||||
server.setHandler(handler);
|
||||
|
||||
handler.addServletWithMapping(WxCpDemoServlet.class, "/*");
|
||||
server.start();
|
||||
server.join();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package me.chanjar.weixin.cp.demo;
|
||||
|
||||
import me.chanjar.weixin.cp.api.*;
|
||||
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;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class WxCpDemoServlet extends HttpServlet {
|
||||
|
||||
protected WxCpService wxCpService;
|
||||
protected WxCpConfigStorage wxCpConfigStorage;
|
||||
protected WxCpMessageRouter wxCpMessageRouter;
|
||||
|
||||
@Override public void init() throws ServletException {
|
||||
//
|
||||
super.init();
|
||||
try {
|
||||
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
|
||||
WxCpDemoInMemoryConfigStorage config = WxCpDemoInMemoryConfigStorage.fromXml(is1);
|
||||
|
||||
wxCpConfigStorage = config;
|
||||
wxCpService = new WxCpServiceImpl();
|
||||
wxCpService.setWxCpConfigStorage(config);
|
||||
|
||||
WxCpMessageHandler handler = new WxCpMessageHandler() {
|
||||
@Override public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context) {
|
||||
WxCpXmlOutTextMessage m = WxCpXmlOutMessage
|
||||
.TEXT()
|
||||
.content("测试加密消息")
|
||||
.fromUser(wxMessage.getToUserName())
|
||||
.toUser(wxMessage.getFromUserName())
|
||||
.build();
|
||||
return m;
|
||||
}
|
||||
};
|
||||
|
||||
wxCpMessageRouter = new WxCpMessageRouter();
|
||||
wxCpMessageRouter
|
||||
.rule()
|
||||
.async(false)
|
||||
.content("哈哈") // 拦截内容为“哈哈”的消息
|
||||
.handler(handler)
|
||||
.end();
|
||||
|
||||
} catch (JAXBException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override protected void service(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
|
||||
String msgSignature = request.getParameter("msg_signature");
|
||||
String nonce = request.getParameter("nonce");
|
||||
String timestamp = request.getParameter("timestamp");
|
||||
String echostr = request.getParameter("echostr");
|
||||
|
||||
response.setContentType("text/html;charset=utf-8");
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user