Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
wecloud_im_server
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hewei
wecloud_im_server
Commits
1cf953ad
Commit
1cf953ad
authored
May 19, 2022
by
罗长华
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
回调签名放置请求头
parent
894e1817
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
33 deletions
+48
-33
core/src/main/java/com/wecloud/im/service/impl/ImCallbackServiceImpl.java
+48
-33
No files found.
core/src/main/java/com/wecloud/im/service/impl/ImCallbackServiceImpl.java
View file @
1cf953ad
...
@@ -2,18 +2,22 @@ package com.wecloud.im.service.impl;
...
@@ -2,18 +2,22 @@ package com.wecloud.im.service.impl;
import
io.geekidea.springbootplus.framework.shiro.signature.SignUtils
;
import
io.geekidea.springbootplus.framework.shiro.signature.SignUtils
;
import
java.net.URI
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Objects
;
import
java.util.Objects
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
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.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.client.RestTemplate
;
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.date.DateUtil
;
import
cn.hutool.core.lang.id.NanoId
;
import
cn.hutool.core.lang.id.NanoId
;
...
@@ -82,18 +86,7 @@ public class ImCallbackServiceImpl implements ImCallbackService {
...
@@ -82,18 +86,7 @@ public class ImCallbackServiceImpl implements ImCallbackService {
String
appKey
=
application
.
getAppKey
();
String
appKey
=
application
.
getAppKey
();
String
appSecret
=
application
.
getAppSecret
();
String
appSecret
=
application
.
getAppSecret
();
try
{
callback
(
subscribeUrl
,
appKey
,
appSecret
,
buildMsgBody
(
conversation
,
message
));
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
}
}
}
return
true
;
return
true
;
}
}
...
@@ -133,17 +126,7 @@ public class ImCallbackServiceImpl implements ImCallbackService {
...
@@ -133,17 +126,7 @@ public class ImCallbackServiceImpl implements ImCallbackService {
.
time
(
time
)
.
time
(
time
)
.
clientIp
(
clientIp
)
.
clientIp
(
clientIp
)
.
build
();
.
build
();
try
{
callback
(
subscribeUrl
,
appKey
,
appSecret
,
body
);
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
}
}
}
return
true
;
return
true
;
}
}
...
@@ -155,21 +138,18 @@ public class ImCallbackServiceImpl implements ImCallbackService {
...
@@ -155,21 +138,18 @@ public class ImCallbackServiceImpl implements ImCallbackService {
* @param
* @param
* @Return
* @Return
*/
*/
private
Map
<
String
,
String
>
buildUriVariables
(
String
appKey
,
String
appSecret
)
{
private
String
buildFinalUrl
(
String
url
,
String
appKey
,
String
appSecret
)
{
Map
<
String
,
String
>
uriVariables
=
new
HashMap
<>();
// 计算 Signature (数据签名)
// 计算 Signature (数据签名)
String
nonce
=
NanoId
.
randomNanoId
();
String
nonce
=
NanoId
.
randomNanoId
();
String
date
=
DateUtil
.
formatHttpDate
(
new
Date
());
String
date
=
DateUtil
.
formatHttpDate
(
new
Date
());
String
signature
=
SignUtils
.
buildSignature
(
appKey
,
appSecret
,
nonce
,
date
);
String
signature
=
SignUtils
.
buildSignature
(
appKey
,
appSecret
,
nonce
,
date
);
uriVariables
.
put
(
"appKey"
,
appKey
);
String
finalUrl
=
uriVariables
.
put
(
"nonce"
,
nonce
);
url
+
"?appKey="
+
appKey
+
"&nonce="
+
nonce
+
"&date="
+
date
+
"&signature="
+
UriEncoder
.
encode
(
signature
);
uriVariables
.
put
(
"date"
,
date
);
return
finalUrl
;
uriVariables
.
put
(
"signature"
,
signature
);
return
uriVariables
;
}
}
private
Boolean
isNeedSync
(
ImConversation
conversation
,
ImMessage
message
)
{
private
Boolean
isNeedSync
(
ImConversation
conversation
,
ImMessage
message
)
{
int
chatType
=
conversation
.
getChatType
();
int
chatType
=
conversation
.
getChatType
();
if
(!(
ChatTypeEnum
.
SINGLE
.
getCode
().
equals
(
chatType
)
||
ChatTypeEnum
.
NORMAL_GROUP
.
getCode
().
equals
(
chatType
)))
{
if
(!(
ChatTypeEnum
.
SINGLE
.
getCode
().
equals
(
chatType
)
||
ChatTypeEnum
.
NORMAL_GROUP
.
getCode
().
equals
(
chatType
)))
{
...
@@ -215,4 +195,39 @@ public class ImCallbackServiceImpl implements ImCallbackService {
...
@@ -215,4 +195,39 @@ public class ImCallbackServiceImpl implements ImCallbackService {
body
.
put
(
"withdrawTime"
,
DateUtil
.
formatDateTime
(
message
.
getWithdrawTime
()));
body
.
put
(
"withdrawTime"
,
DateUtil
.
formatDateTime
(
message
.
getWithdrawTime
()));
return
body
;
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
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment