Commit 1cf953ad by 罗长华

回调签名放置请求头

parent 894e1817
......@@ -2,18 +2,22 @@ package com.wecloud.im.service.impl;
import io.geekidea.springbootplus.framework.shiro.signature.SignUtils;
import java.net.URI;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import org.yaml.snakeyaml.util.UriEncoder;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.id.NanoId;
......@@ -82,18 +86,7 @@ public class ImCallbackServiceImpl implements ImCallbackService {
String appKey = application.getAppKey();
String appSecret = application.getAppSecret();
try {
ResponseEntity<Object> response = restTemplate.postForEntity(subscribeUrl,
buildMsgBody(conversation, message), Object.class, buildUriVariables(appKey, appSecret));
// 同步在线状态时需要接收服务提供应答,只要有 HTTP 应答码 200 即认为状态已经同步
if (response.getStatusCode().equals(HttpStatus.OK)) {
// do nothing
} else {
// 如果应答超时 5 秒,会再尝试推送 2 次,如果仍然失败,将不再同步此条状态。如短时间内有大面积超时,将暂停推送,1 分钟后会继续推送。
}
} catch (Exception e) {
// do nothing is ok
}
callback(subscribeUrl, appKey, appSecret, buildMsgBody(conversation, message));
}
return true;
}
......@@ -133,17 +126,7 @@ public class ImCallbackServiceImpl implements ImCallbackService {
.time(time)
.clientIp(clientIp)
.build();
try {
ResponseEntity<Object> response = restTemplate.postForEntity(subscribeUrl, body, Object.class, buildUriVariables(appKey, appSecret));
// 同步在线状态时需要接收服务提供应答,只要有 HTTP 应答码 200 即认为状态已经同步
if (response.getStatusCode().equals(HttpStatus.OK)) {
// do nothing
} else {
// 如果应答超时 5 秒,会再尝试推送 2 次,如果仍然失败,将不再同步此条状态。如短时间内有大面积超时,将暂停推送,1 分钟后会继续推送。
}
} catch (Exception e) {
// do nothing is ok
}
callback(subscribeUrl, appKey, appSecret, body);
}
return true;
}
......@@ -155,21 +138,18 @@ public class ImCallbackServiceImpl implements ImCallbackService {
* @param
* @Return
*/
private Map<String, String> buildUriVariables(String appKey, String appSecret) {
Map<String, String> uriVariables = new HashMap<>();
private String buildFinalUrl(String url, String appKey, String appSecret) {
// 计算 Signature (数据签名)
String nonce = NanoId.randomNanoId();
String date = DateUtil.formatHttpDate(new Date());
String signature = SignUtils.buildSignature(appKey, appSecret, nonce, date);
uriVariables.put("appKey", appKey);
uriVariables.put("nonce", nonce);
uriVariables.put("date", date);
uriVariables.put("signature", signature);
return uriVariables;
String finalUrl =
url + "?appKey=" + appKey + "&nonce=" + nonce + "&date=" + date + "&signature=" + UriEncoder.encode(signature);
return finalUrl;
}
private Boolean isNeedSync(ImConversation conversation, ImMessage message) {
int chatType = conversation.getChatType();
if (!(ChatTypeEnum.SINGLE.getCode().equals(chatType) || ChatTypeEnum.NORMAL_GROUP.getCode().equals(chatType))) {
......@@ -215,4 +195,39 @@ public class ImCallbackServiceImpl implements ImCallbackService {
body.put("withdrawTime", DateUtil.formatDateTime(message.getWithdrawTime()));
return body;
}
private void callback(String subscribeUrl, String appKey, String appSecret, Object body) {
try {
UriComponentsBuilder uriComponentsBuilder =
UriComponentsBuilder.fromHttpUrl(subscribeUrl);
URI uri = uriComponentsBuilder.build(true).toUri();
HttpHeaders headers = buildHeader(subscribeUrl, appKey, appSecret);
HttpEntity<Object> entity = new HttpEntity<>(body, headers);
ResponseEntity<Object> response = restTemplate.exchange(uri, HttpMethod.POST, entity, Object.class);
// 同步在线状态时需要接收服务提供应答,只要有 HTTP 应答码 200 即认为状态已经同步
if (response.getStatusCode().equals(HttpStatus.OK)) {
// do nothing
} else {
// 如果应答超时 5 秒,会再尝试推送 2 次,如果仍然失败,将不再同步此条状态。如短时间内有大面积超时,将暂停推送,1 分钟后会继续推送。
}
} catch (Exception e) {
// do nothing is ok
}
}
private HttpHeaders buildHeader(String url, String appKey, String appSecret) {
// 计算 Signature (数据签名)
String nonce = NanoId.randomNanoId();
String date = DateUtil.formatHttpDate(new Date());
String signature = SignUtils.buildSignature(appKey, appSecret, nonce, date);
HttpHeaders headers = new HttpHeaders();
headers.add("WECLOUD-IM-APPKEY", appKey);
headers.add("WECLOUD-IM-NONCE", nonce);
headers.add("WECLOUD-IM-DATE", date);
headers.add("WECLOUD-IM-SIGNATURE", signature);
return headers;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment