issue #39 企业号添加oauth2的支持
This commit is contained in:
@@ -1,20 +1,93 @@
|
||||
package me.chanjar.weixin.cp.demo;
|
||||
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
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 org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.servlet.ServletHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
|
||||
import javax.xml.bind.JAXBException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class WxCpDemoServer {
|
||||
|
||||
private static WxCpConfigStorage wxCpConfigStorage;
|
||||
private static WxCpService wxCpService;
|
||||
private static WxCpMessageRouter wxCpMessageRouter;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Server server = new Server(8080);
|
||||
|
||||
ServletHandler handler = new ServletHandler();
|
||||
server.setHandler(handler);
|
||||
|
||||
handler.addServletWithMapping(WxCpDemoServlet.class, "/*");
|
||||
ServletHolder endpointServletHolder = new ServletHolder(new WxCpEndpointServlet(wxCpConfigStorage, wxCpService, wxCpMessageRouter));
|
||||
handler.addServletWithMapping(endpointServletHolder, "/*");
|
||||
|
||||
ServletHolder oauthServletHolder = new ServletHolder(new WxCpOAuth2Servlet(wxCpService));
|
||||
handler.addServletWithMapping(oauthServletHolder, "/oauth2/*");
|
||||
|
||||
server.start();
|
||||
server.join();
|
||||
}
|
||||
|
||||
private static void 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,
|
||||
WxCpService wxCpService) {
|
||||
WxCpXmlOutTextMessage m = WxCpXmlOutMessage
|
||||
.TEXT()
|
||||
.content("测试加密消息")
|
||||
.fromUser(wxMessage.getToUserName())
|
||||
.toUser(wxMessage.getFromUserName())
|
||||
.build();
|
||||
return m;
|
||||
}
|
||||
};
|
||||
|
||||
WxCpMessageHandler oauth2handler = new WxCpMessageHandler() {
|
||||
@Override
|
||||
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context,
|
||||
WxCpService wxCpService) {
|
||||
String href = "<a href=\"" + wxCpService.oauth2buildAuthorizationUrl(null)
|
||||
+ "\">测试oauth2</a>";
|
||||
return WxCpXmlOutMessage
|
||||
.TEXT()
|
||||
.content(href)
|
||||
.fromUser(wxMessage.getToUserName())
|
||||
.toUser(wxMessage.getFromUserName()).build();
|
||||
}
|
||||
};
|
||||
|
||||
wxCpMessageRouter = new WxCpMessageRouter(wxCpService);
|
||||
wxCpMessageRouter
|
||||
.rule()
|
||||
.async(false)
|
||||
.content("哈哈") // 拦截内容为“哈哈”的消息
|
||||
.handler(handler)
|
||||
.end()
|
||||
.rule()
|
||||
.async(false)
|
||||
.content("oauth")
|
||||
.handler(oauth2handler)
|
||||
.end()
|
||||
;
|
||||
|
||||
} catch (JAXBException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,49 +19,21 @@ import java.util.Map;
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class WxCpDemoServlet extends HttpServlet {
|
||||
public class WxCpEndpointServlet extends HttpServlet {
|
||||
|
||||
protected WxCpConfigStorage wxCpConfigStorage;
|
||||
protected WxCpService wxCpService;
|
||||
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, 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();
|
||||
|
||||
} catch (JAXBException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
public WxCpEndpointServlet(WxCpConfigStorage wxCpConfigStorage, WxCpService wxCpService,
|
||||
WxCpMessageRouter wxCpMessageRouter) {
|
||||
this.wxCpConfigStorage = wxCpConfigStorage;
|
||||
this.wxCpService = wxCpService;
|
||||
this.wxCpMessageRouter = wxCpMessageRouter;
|
||||
}
|
||||
|
||||
@Override protected void service(HttpServletRequest request, HttpServletResponse response)
|
||||
@Override
|
||||
protected void service(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
|
||||
response.setContentType("text/html;charset=utf-8");
|
||||
@@ -85,7 +57,8 @@ public class WxCpDemoServlet extends HttpServlet {
|
||||
return;
|
||||
}
|
||||
|
||||
WxCpXmlMessage inMessage = WxCpXmlMessage.fromEncryptedXml(request.getInputStream(), wxCpConfigStorage, timestamp, nonce, msgSignature);
|
||||
WxCpXmlMessage inMessage = WxCpXmlMessage
|
||||
.fromEncryptedXml(request.getInputStream(), wxCpConfigStorage, timestamp, nonce, msgSignature);
|
||||
WxCpXmlOutMessage outMessage = wxCpMessageRouter.route(inMessage);
|
||||
if (outMessage != null) {
|
||||
response.getWriter().write(outMessage.toEncryptedXml(wxCpConfigStorage));
|
||||
@@ -0,0 +1,48 @@
|
||||
package me.chanjar.weixin.cp.demo;
|
||||
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.cp.api.WxCpService;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by qianjia on 14/11/28.
|
||||
*/
|
||||
public class WxCpOAuth2Servlet extends HttpServlet {
|
||||
|
||||
protected WxCpService wxCpService;
|
||||
|
||||
public WxCpOAuth2Servlet(WxCpService wxCpService) {
|
||||
this.wxCpService = wxCpService;
|
||||
}
|
||||
|
||||
@Override protected void service(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
|
||||
response.setContentType("text/html;charset=utf-8");
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
|
||||
String code = request.getParameter("code");
|
||||
try {
|
||||
response.getWriter().println("<h1>code</h1>");
|
||||
response.getWriter().println(code);
|
||||
|
||||
String[] res = wxCpService.oauth2getUserInfo(code);
|
||||
response.getWriter().println("<h1>result</h1>");
|
||||
response.getWriter().println(Arrays.toString(res));
|
||||
} catch (WxErrorException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
response.getWriter().flush();
|
||||
response.getWriter().close();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -9,4 +9,5 @@
|
||||
<userId>企业号通讯录里的某个userid</userId>
|
||||
<departmentId>企业号通讯录的某个部门id</departmentId>
|
||||
<tagId>企业号通讯录里的某个tagid</tagId>
|
||||
<oauth2redirectUri>网页授权获取用户信息回调地址</oauth2redirectUri>
|
||||
</xml>
|
||||
|
||||
Reference in New Issue
Block a user