前言
最近实现社群对接企业微信,对接的过程遇到一些点,在此记录。
企业微信介绍
企业微信具有和微信一样的体验,用于企业内部成员和外部客户的管理,可以由此构建出社群生态。
企业微信提供了丰富的api进行调用获取数据管理,也提供了各种回调事件,当数据发生变化时,可以及时知道。
我们分为两部分进行讲解,第一部分调用企业微信api,第二部分,接收企业微信的回调。
调用企业微信api
api的开发文档地址:
https://work.weixin.qq.com/api/doc/90000/90135/90664
调用企业微信所必须的东西就是企业的accesstoken。获取accesstoken则需要我们的corpid和corpsercret。
具体我们可以参照这里
https://work.weixin.qq.com/api/doc/90000/90135/91039
有了token之后,我们就可以通过http请求来调用各种api,获取数据。举一个例子,创建成员的api,如下,我们只要使用http工具调用即可。
这里分享一个http调用工具。
@Slf4j
public class HttpUtils {
static CloseableHttpClient httpClient;
private HttpUtils() {
throw new IllegalStateException("Utility class");
}
static {
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
connectionManager.setMaxTotal(200);
connectionManager.setDefaultMaxPerRoute(200);
connectionManager.setDefaultSocketConfig(
SocketConfig.custom().setSoTimeout(15, TimeUnit.SECONDS)
.setTcpNoDelay(true).build()
);
connectionManager.setValidateAfterInactivity(TimeValue.ofSeconds(15));
httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.disableAutomaticRetries()
.build();
}
public static String get(String url, Map<String, Object> paramMap, Map<String, String> headerMap) {
String param = paramMap.entrySet().stream().map(n -> n.getKey() + "=" + n.getValue()).collect(Collectors.joining("&"));
String fullUrl = url + "?" + param;
final HttpGet httpGet = new HttpGet(fullUrl);
if (Objects.nonNull(headerMap) && headerMap.size() > 0) {
headerMap.forEach((key, value) -> httpGet.addHeader(key, value));
}
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
String strResult = EntityUtils.toString(response.getEntity());
if (200 != response.getCode()) {
log.error("HTTP get 返回状态非200[resp={}]", strResult);
}
return strResult;
} catch (IOException | ParseException e) {
log.error("HTTP get 异常", e);
return "";
} finally {
if (null != response) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static String post(String url,Map<String, Object> paramMap, Map<String, String> headerMap, String data) {
CloseableHttpResponse response = null;
try {
String param = paramMap.entrySet().stream().map(n -> n.getKey() + "=" + n.getValue()).collect(Collectors.joining("&"));
String fullUrl = url + "?" + param;
final HttpPost httpPost = new HttpPost(fullUrl);
if (Objects.nonNull(headerMap) && headerMap.size() > 0) {
headerMap.forEach((key, value) -> httpPost.addHeader(key, value));
}
StringEntity httpEntity = new StringEntity(data, StandardCharsets.UTF_8);
httpPost.setEntity(httpEntity);
response = httpClient.execute(httpPost);
if (200 == response.getCode()) {
String strResult = EntityUtils.toString(response.getEntity());
return strResult;
}
} catch (IOException | ParseException e) {
e.printStackTrace();
return "";
} finally {
if (null != response) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "";
}
}
对接企业微信的回调
回调分为很多种,比如通讯录的回调如下:
https://work.weixin.qq.com/api/doc/90000/90135/90967
整体的回调流程如下:
配置回调服务,需要有三个配置项,分别是:URL, Token, EncodingAESKey。
首先,URL为回调服务地址,由开发者搭建,用于接收通知消息或者事件。
其次,Token用于计算签名,由英文或数字组成且长度不超过32位的自定义字符串。开发者提供的URL是公开可访问的,这就意味着拿到这个URL,就可以往该链接推送消息。那么URL服务需要解决两个问题:
如何分辨出是否为企业微信来源
如何分辨出推送消息的内容是否被篡改
通过数字签名就可以解决上述的问题。具体为:约定Token作为密钥,仅开发者和企业微信知道,在传输中不可见,用于参与签名计算。企业微信在推送消息时,将消息内容与Token计算出签名。开发者接收到推送消息时,也按相同算法计算出签名。如果为同一签名,则可信任来源为企业微信,并且内容是完整的。
如果非企业微信来源,由于攻击者没有正确的Token,无法算出正确的签名;
如果消息内容被篡改,由于开发者会将接收的消息内容与Token重算一次签名,该值与参数的签名不一致,则会拒绝该请求。