Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
szpt
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
以墨为白
szpt
Commits
7be11259
Commit
7be11259
authored
Jan 10, 2025
by
以墨为白
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化
parent
60aca3dc
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
103 additions
and
137 deletions
+103
-137
src/main/java/com/zksy/szpt/aspect/RequestBodyAnalysis.java
src/main/java/com/zksy/szpt/aspect/RequestBodyAnalysis.java
+1
-1
src/main/java/com/zksy/szpt/filter/SignatureVerificationFilter1.java
...va/com/zksy/szpt/filter/SignatureVerificationFilter1.java
+1
-2
src/test/java/com/zksy/szpt/CommonUtil.java
src/test/java/com/zksy/szpt/CommonUtil.java
+0
-4
src/test/java/com/zksy/szpt/TestAppStore.java
src/test/java/com/zksy/szpt/TestAppStore.java
+4
-130
src/test/java/com/zksy/szpt/TestHttpUtil.java
src/test/java/com/zksy/szpt/TestHttpUtil.java
+97
-0
No files found.
src/main/java/com/zksy/szpt/aspect/RequestBodyAnalysis.java
View file @
7be11259
...
@@ -45,7 +45,7 @@ public class RequestBodyAnalysis implements RequestBodyAdvice {
...
@@ -45,7 +45,7 @@ public class RequestBodyAnalysis implements RequestBodyAdvice {
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
IOUtils
.
copy
(
httpInputMessage
.
getBody
(),
baos
);
IOUtils
.
copy
(
httpInputMessage
.
getBody
(),
baos
);
CommonRequestDTO
commonRequestDTO
=
objectMapper
.
readValue
(
new
String
(
baos
.
toByteArray
(),
StandardCharsets
.
UTF_8
),
CommonRequestDTO
.
class
);
CommonRequestDTO
commonRequestDTO
=
objectMapper
.
readValue
(
new
String
(
baos
.
toByteArray
(),
StandardCharsets
.
UTF_8
),
CommonRequestDTO
.
class
);
String
content
=
EncryptUtil
.
getInstance
().
AESDecode
(
commonRequestDTO
.
getContent
(),
"c81e728d9d4c2f636f067f89cc14862c"
);
String
content
=
EncryptUtil
.
getInstance
().
AESDecode
(
commonRequestDTO
.
getContent
(),
"c81e728d9d4c2f636f067f89cc14862c"
);
//todo
return
new
ByteArrayInputStream
(
content
.
getBytes
(
StandardCharsets
.
UTF_8
));
return
new
ByteArrayInputStream
(
content
.
getBytes
(
StandardCharsets
.
UTF_8
));
}
}
};
};
...
...
src/main/java/com/zksy/szpt/filter/SignatureVerificationFilter1.java
View file @
7be11259
...
@@ -18,6 +18,7 @@ import org.slf4j.Logger;
...
@@ -18,6 +18,7 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.MediaType
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.filter.OncePerRequestFilter
;
import
org.springframework.web.filter.OncePerRequestFilter
;
...
@@ -26,11 +27,9 @@ import javax.servlet.FilterChain;
...
@@ -26,11 +27,9 @@ import javax.servlet.FilterChain;
import
javax.servlet.ServletException
;
import
javax.servlet.ServletException
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.nio.charset.StandardCharsets
;
import
java.nio.charset.StandardCharsets
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
import
java.util.stream.Collectors
;
/**
/**
* 请求body { appkey , method , hotelCode , content(根据密钥[md5]对dto的json加密aes), sign(前面的参数一起哈希运算)} 。 无其他参数
* 请求body { appkey , method , hotelCode , content(根据密钥[md5]对dto的json加密aes), sign(前面的参数一起哈希运算)} 。 无其他参数
...
...
src/test/java/com/zksy/szpt/CommonUtil.java
deleted
100644 → 0
View file @
60aca3dc
package
com
.
zksy
.
szpt
;
public
class
CommonUtil
{
}
src/test/java/com/zksy/szpt/TestAppStore.java
View file @
7be11259
package
com
.
zksy
.
szpt
;
package
com
.
zksy
.
szpt
;
import
cn.hutool.crypto.digest.DigestUtil
;
import
cn.hutool.crypto.digest.DigestUtil
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.zksy.szpt.domain.CommonRequestDTO
;
import
com.zksy.szpt.domain.dto.*
;
import
com.zksy.szpt.domain.dto.*
;
import
com.zksy.szpt.util.EncryptUtil
;
import
com.zksy.szpt.util.SignatureUtil
;
import
org.junit.jupiter.api.Assertions
;
import
org.junit.jupiter.api.Assertions
;
import
org.junit.jupiter.api.DisplayName
;
import
org.junit.jupiter.api.DisplayName
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.web.reactive.function.client.WebClient
;
import
reactor.core.publisher.Mono
;
import
javax.annotation.Resource
;
@SpringBootTest
()
@SpringBootTest
()
public
class
TestAppStore
{
public
class
TestAppStore
{
String
nonce
=
"2"
;
String
timestampStr
=
"21"
;
String
appId
=
"1872576325743943682"
;
String
appSecret
=
"2"
;
@Resource
private
ObjectMapper
objectMapper
;
/**
/**
* 新增AppId
* 新增AppId
*/
*/
@Test
@Test
@DisplayName
(
"新增AppId"
)
@DisplayName
(
"新增AppId"
)
public
void
addAppId
()
{
public
void
addAppId
()
{
timestampStr
=
String
.
valueOf
(
System
.
currentTimeMillis
()
/
1000
);
nonce
=
String
.
valueOf
(
System
.
currentTimeMillis
()
/
1000
);
appSecret
=
DigestUtil
.
md5Hex
(
appSecret
);
Assertions
.
assertNotNull
(
appId
,
"appId不存在"
);
//断言appId存在,为空直接抛出异常不进行下一步测试,提高测试效率
//请求参数
AppStoreDTO
appStoreDTO
=
new
AppStoreDTO
();
AppStoreDTO
appStoreDTO
=
new
AppStoreDTO
();
appStoreDTO
.
setAppKey
(
"us2"
);
appStoreDTO
.
setAppKey
(
"us2"
);
appStoreDTO
.
setAppSecret
(
DigestUtil
.
md5Hex
(
"us"
));
appStoreDTO
.
setAppSecret
(
DigestUtil
.
md5Hex
(
"us"
));
appStoreDTO
.
setDeptCode
(
"123456"
);
appStoreDTO
.
setDeptCode
(
"123456"
);
String
json
=
null
;
TestHttpUtil
.
signatureAndRequest
(
"/rest/appStore/insertAppStore"
,
appStoreDTO
,
AppStoreDTO
.
class
);
try
{
// TestHttpUtil.signatureAndRequest("/rest/appStore/insertAppStore", appStoreDTO);
json
=
objectMapper
.
writeValueAsString
(
appStoreDTO
);
}
catch
(
JsonProcessingException
e
)
{
Assertions
.
fail
(
"json序列化失败"
);
}
//请求体加密
json
=
EncryptUtil
.
getInstance
().
AESEncode
(
json
,
appSecret
);
//签名appId+nonce+timestampStr+aes(body)
String
data
=
String
.
format
(
"%s%s%s%s"
,
appId
,
nonce
,
timestampStr
,
json
);
String
generatedSignature
=
DigestUtil
.
md5Hex
(
data
);
//请求
WebClient
webClient
=
WebClient
.
builder
()
.
baseUrl
(
"http://localhost:8086"
)
.
defaultHeader
(
"Content-Type"
,
"application/json"
)
.
build
();
String
response
=
webClient
.
post
().
uri
(
"/rest/appStore/insertAppStore"
)
.
header
(
SignatureUtil
.
APPID
,
appId
)
.
header
(
SignatureUtil
.
NONCE
,
nonce
)
.
header
(
SignatureUtil
.
TIMESTAMP
,
timestampStr
)
.
header
(
SignatureUtil
.
SIGNATURE
,
generatedSignature
)
.
body
(
Mono
.
just
(
appStoreDTO
),
AppStoreDTO
.
class
)
.
retrieve
()
.
bodyToMono
(
String
.
class
)
.
block
();
System
.
out
.
println
(
response
);
}
}
@Test
@Test
@DisplayName
(
"更新AppId的密钥"
)
@DisplayName
(
"更新AppId的密钥"
)
public
void
updateAppIdSecret
()
{
public
void
updateAppIdSecret
()
{
timestampStr
=
String
.
valueOf
(
System
.
currentTimeMillis
()
/
1000
);
nonce
=
String
.
valueOf
(
System
.
currentTimeMillis
()
/
1000
);
appSecret
=
DigestUtil
.
md5Hex
(
appSecret
);
Assertions
.
assertNotNull
(
appId
,
"appId不存在"
);
//断言appId存在,为空直接抛出异常不进行下一步测试,提高测试效率
//请求参数
AppStoreDTO
appStoreDTO
=
new
AppStoreDTO
();
appStoreDTO
.
setAppKey
(
"us"
);
appStoreDTO
.
setAppSecret
(
DigestUtil
.
md5Hex
(
"us11"
));
String
json
=
null
;
try
{
json
=
objectMapper
.
writeValueAsString
(
appStoreDTO
);
}
catch
(
JsonProcessingException
e
)
{
Assertions
.
fail
(
"json序列化失败"
);
}
//请求体加密
json
=
EncryptUtil
.
getInstance
().
AESEncode
(
json
,
appSecret
);
//签名appId+nonce+timestampStr+aes(body)
String
data
=
String
.
format
(
"%s%s%s%s"
,
appId
,
nonce
,
timestampStr
,
json
);
String
generatedSignature
=
DigestUtil
.
md5Hex
(
data
);
//请求
WebClient
webClient
=
WebClient
.
builder
()
.
baseUrl
(
"http://localhost:8086"
)
.
defaultHeader
(
"Content-Type"
,
"application/json"
)
.
build
();
String
response
=
webClient
.
post
().
uri
(
"/rest/appStore/updateAppIdSecret"
)
.
header
(
SignatureUtil
.
APPID
,
appId
)
.
header
(
SignatureUtil
.
NONCE
,
nonce
)
.
header
(
SignatureUtil
.
TIMESTAMP
,
timestampStr
)
.
header
(
SignatureUtil
.
SIGNATURE
,
generatedSignature
)
.
body
(
Mono
.
just
(
appStoreDTO
),
XxRwwcqkDTO
.
class
)
.
retrieve
()
.
bodyToMono
(
String
.
class
)
.
block
();
System
.
out
.
println
(
response
);
}
/**
* 新增AppId
*/
@Test
@DisplayName
(
"新增AppId1"
)
public
void
addAppId1
()
{
String
deptCode
=
"330102"
;
timestampStr
=
String
.
valueOf
(
System
.
currentTimeMillis
()
/
1000
);
nonce
=
String
.
valueOf
(
System
.
currentTimeMillis
()
/
1000
);
appSecret
=
DigestUtil
.
md5Hex
(
appSecret
);
Assertions
.
assertNotNull
(
appId
,
"appId不存在"
);
//断言appId存在,为空直接抛出异常不进行下一步测试,提高测试效率
//请求参数
AppStoreDTO
appStoreDTO
=
new
AppStoreDTO
();
AppStoreDTO
appStoreDTO
=
new
AppStoreDTO
();
appStoreDTO
.
setAppKey
(
"us2"
);
appStoreDTO
.
setAppKey
(
"us2"
);
appStoreDTO
.
setAppSecret
(
DigestUtil
.
md5Hex
(
"us"
));
appStoreDTO
.
setAppSecret
(
DigestUtil
.
md5Hex
(
"asdfasdfadf11"
));
appStoreDTO
.
setDeptCode
(
deptCode
);
TestHttpUtil
.
signatureAndRequest
(
"/rest/appStore/updateAppIdSecret"
,
appStoreDTO
,
AppStoreDTO
.
class
);
String
json
=
null
;
try
{
json
=
objectMapper
.
writeValueAsString
(
appStoreDTO
);
}
catch
(
JsonProcessingException
e
)
{
Assertions
.
fail
(
"json序列化失败"
);
}
//请求体加密
json
=
EncryptUtil
.
getInstance
().
AESEncode
(
json
,
appSecret
);
//签名appId+nonce+timestampStr+aes(body)+deptCode
String
data
=
String
.
format
(
"%s%s%s%s%s"
,
appId
,
nonce
,
timestampStr
,
json
,
deptCode
);
String
generatedSignature
=
DigestUtil
.
md5Hex
(
data
);
CommonRequestDTO
commonRequestDTO
=
new
CommonRequestDTO
();
commonRequestDTO
.
setAppId
(
appId
);
commonRequestDTO
.
setNonce
(
nonce
);
commonRequestDTO
.
setTimestamp
(
timestampStr
);
commonRequestDTO
.
setDeptCode
(
deptCode
);
commonRequestDTO
.
setContent
(
json
);
commonRequestDTO
.
setSign
(
generatedSignature
);
//请求
WebClient
webClient
=
WebClient
.
builder
()
.
baseUrl
(
"http://localhost:8086"
)
.
defaultHeader
(
"Content-Type"
,
"application/json"
)
.
build
();
String
response
=
webClient
.
post
().
uri
(
"/rest/appStore/insertAppStore"
)
.
body
(
Mono
.
just
(
commonRequestDTO
),
CommonRequestDTO
.
class
)
.
retrieve
()
.
bodyToMono
(
String
.
class
)
.
block
();
System
.
out
.
println
(
response
);
}
}
}
}
src/test/java/com/zksy/szpt/TestHttpUtil.java
0 → 100644
View file @
7be11259
package
com
.
zksy
.
szpt
;
import
cn.hutool.crypto.digest.DigestUtil
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.zksy.szpt.domain.CommonRequestDTO
;
import
com.zksy.szpt.util.EncryptUtil
;
import
com.zksy.szpt.util.SignatureUtil
;
import
org.junit.jupiter.api.Assertions
;
import
org.springframework.web.reactive.function.client.WebClient
;
import
reactor.core.publisher.Mono
;
public
class
TestHttpUtil
{
private
static
final
org
.
slf4j
.
Logger
log
=
org
.
slf4j
.
LoggerFactory
.
getLogger
(
TestHttpUtil
.
class
);
static
String
nonce
=
"2"
;
static
String
timestampStr
=
"21"
;
static
String
appId
=
"1872576325743943682"
;
static
String
appSecret
=
"2"
;
static
String
deptCode
=
"330102"
;
private
static
final
ObjectMapper
objectMapper
=
new
ObjectMapper
();
//body {单位, 房间, 人员信息数组} 请求头加nonce timestamp appId sign(body[遍历按照key按照顺序&拼接] 加 nonece timestamp appId 用$符号拼接)
public
static
<
E
,
T
>
void
signatureAndRequest
(
String
path
,
T
source
,
Class
<
E
>
destinationClass
)
{
Assertions
.
assertNotNull
(
path
,
"path不能为空"
);
Assertions
.
assertNotNull
(
source
,
"请求参数不能为空"
);
timestampStr
=
String
.
valueOf
(
System
.
currentTimeMillis
()
/
1000
);
nonce
=
String
.
valueOf
(
System
.
currentTimeMillis
()
/
1000
);
appSecret
=
DigestUtil
.
md5Hex
(
appSecret
);
String
json
=
null
;
try
{
json
=
objectMapper
.
writeValueAsString
(
source
);
}
catch
(
JsonProcessingException
e
)
{
Assertions
.
fail
(
"json序列化失败"
);
}
//请求体加密
json
=
EncryptUtil
.
getInstance
().
AESEncode
(
json
,
appSecret
);
//签名appId+nonce+timestampStr+aes(body)
String
data
=
String
.
format
(
"%s%s%s%s"
,
appId
,
nonce
,
timestampStr
,
json
);
String
generatedSignature
=
DigestUtil
.
md5Hex
(
data
);
//请求
WebClient
webClient
=
WebClient
.
builder
()
.
baseUrl
(
"http://localhost:8086"
)
.
defaultHeader
(
"Content-Type"
,
"application/json"
)
.
build
();
String
response
=
webClient
.
post
().
uri
(
path
)
.
header
(
SignatureUtil
.
APPID
,
appId
)
.
header
(
SignatureUtil
.
NONCE
,
nonce
)
.
header
(
SignatureUtil
.
TIMESTAMP
,
timestampStr
)
.
header
(
SignatureUtil
.
SIGNATURE
,
generatedSignature
)
.
body
(
Mono
.
just
(
source
),
destinationClass
)
.
retrieve
()
.
bodyToMono
(
String
.
class
)
.
block
();
log
.
info
(
"response: {}"
,
response
);
}
//body { appkey , method , hotelCode , content(根据密钥[md5]对dto的json加密aes), sign(前面的参数一起哈希运算)} 。 无其他参数
public
static
<
T
>
void
signatureAndRequest
(
String
path
,
T
source
)
{
Assertions
.
assertNotNull
(
path
,
"path不能为空"
);
Assertions
.
assertNotNull
(
source
,
"请求参数不能为空"
);
timestampStr
=
String
.
valueOf
(
System
.
currentTimeMillis
()
/
1000
);
nonce
=
String
.
valueOf
(
System
.
currentTimeMillis
()
/
1000
);
appSecret
=
DigestUtil
.
md5Hex
(
appSecret
);
String
json
=
null
;
try
{
json
=
objectMapper
.
writeValueAsString
(
source
);
}
catch
(
JsonProcessingException
e
)
{
Assertions
.
fail
(
"json序列化失败"
);
}
//请求体加密
//请求体加密
json
=
EncryptUtil
.
getInstance
().
AESEncode
(
json
,
appSecret
);
//签名appId+nonce+timestampStr+aes(body)+deptCode
String
data
=
String
.
format
(
"%s%s%s%s%s"
,
appId
,
nonce
,
timestampStr
,
json
,
deptCode
);
String
generatedSignature
=
DigestUtil
.
md5Hex
(
data
);
CommonRequestDTO
commonRequestDTO
=
new
CommonRequestDTO
();
commonRequestDTO
.
setAppId
(
appId
);
commonRequestDTO
.
setNonce
(
nonce
);
commonRequestDTO
.
setTimestamp
(
timestampStr
);
commonRequestDTO
.
setDeptCode
(
deptCode
);
commonRequestDTO
.
setContent
(
json
);
commonRequestDTO
.
setSign
(
generatedSignature
);
//请求
WebClient
webClient
=
WebClient
.
builder
()
.
baseUrl
(
"http://localhost:8086"
)
.
defaultHeader
(
"Content-Type"
,
"application/json"
)
.
build
();
String
response
=
webClient
.
post
().
uri
(
path
)
.
body
(
Mono
.
just
(
commonRequestDTO
),
CommonRequestDTO
.
class
)
.
retrieve
()
.
bodyToMono
(
String
.
class
)
.
block
();
log
.
info
(
"response: {}"
,
response
);
}
}
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