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
86e5d1b2
Commit
86e5d1b2
authored
Dec 28, 2024
by
以墨为白
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
测试服务
parent
b1ab4f43
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
11 deletions
+82
-11
pom.xml
pom.xml
+5
-0
src/main/java/com/zksy/szpt/filter/SignatureVerificationFilter.java
...ava/com/zksy/szpt/filter/SignatureVerificationFilter.java
+10
-3
src/test/java/com/zksy/szpt/MainTest.java
src/test/java/com/zksy/szpt/MainTest.java
+67
-8
No files found.
pom.xml
View file @
86e5d1b2
...
...
@@ -93,6 +93,11 @@
<version>
${druid.version}
</version>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-webflux
</artifactId>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-lang3
</artifactId>
...
...
src/main/java/com/zksy/szpt/filter/SignatureVerificationFilter.java
View file @
86e5d1b2
...
...
@@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import
com.zksy.szpt.domain.HttpResult
;
import
com.zksy.szpt.domain.HttpResultState
;
import
com.zksy.szpt.service.AppStoreService
;
import
com.zksy.szpt.util.EncryptUtil
;
import
com.zksy.szpt.util.SignatureUtil
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
...
...
@@ -16,6 +17,7 @@ import org.springframework.stereotype.Component;
import
org.springframework.util.StreamUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.filter.OncePerRequestFilter
;
import
javax.annotation.Resource
;
import
javax.servlet.FilterChain
;
import
javax.servlet.ServletException
;
...
...
@@ -74,6 +76,10 @@ public class SignatureVerificationFilter extends OncePerRequestFilter {
}
String
secretKey
=
this
.
appStoreService
.
getAppSecretByAppKey
(
appId
);
if
(!
StringUtils
.
hasText
(
secretKey
))
{
this
.
write
(
response
,
"appId无效"
);
return
false
;
}
// timestamp 10分钟内有效
// long timestamp = Long.parseLong(timestampStr);
...
...
@@ -94,11 +100,12 @@ public class SignatureVerificationFilter extends OncePerRequestFilter {
// 请求体
String
body
=
StreamUtils
.
copyToString
(
request
.
getInputStream
(),
StandardCharsets
.
UTF_8
);
body
=
objectMapper
.
writeValueAsString
(
objectMapper
.
readValue
(
body
,
Map
.
class
));
// 校验签名appId+nonce+timestampStr+body+secretKey
String
data
=
String
.
format
(
"%s%s%s%s%s"
,
appId
,
nonce
,
timestampStr
,
body
,
secretKey
);
body
=
EncryptUtil
.
getInstance
().
AESEncode
(
body
,
secretKey
);
// 校验签名appId+nonce+timestampStr+aes(body,secret)
String
data
=
String
.
format
(
"%s%s%s%s"
,
appId
,
nonce
,
timestampStr
,
body
);
String
generatedSignature
=
DigestUtil
.
md5Hex
(
data
);
if
(!
generatedSignature
.
equals
(
sign
))
{
write
(
response
,
"签名有误
"
);
write
(
response
,
"签名有误
,generatedSignature:"
+
generatedSignature
+
",sign: "
+
sign
+
",appId:"
+
appId
+
",nonce:"
+
nonce
+
",timestamp:"
+
timestampStr
);
return
false
;
}
return
true
;
...
...
src/test/java/com/zksy/szpt/MainTest.java
View file @
86e5d1b2
package
com
.
zksy
.
szpt
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
import
cn.hutool.crypto.digest.DigestUtil
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.zksy.szpt.domain.dto.XxRwwcqkDTO
;
import
com.zksy.szpt.service.AppStoreService
;
import
com.zksy.szpt.util.EncryptUtil
;
import
com.zksy.szpt.util.SignatureUtil
;
import
org.junit.jupiter.api.Assertions
;
import
org.junit.jupiter.api.DisplayName
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.web.reactive.function.client.WebClient
;
import
reactor.core.publisher.Mono
;
import
javax.annotation.Resource
;
/**
* A simple unit test
*/
public
class
MainTest
{
@SpringBootTest
()
public
class
MainTest
{
String
nonce
=
"nonce1"
;
String
timestampStr
=
"21"
;
String
appId
=
"1"
;
@Resource
private
ObjectMapper
objectMapper
;
@Resource
private
AppStoreService
appStoreService
;
/**
*
Rigorous Test :-)
*
任务完成情况
*/
@Test
public
void
shouldAnswerWithTrue
()
{
assertTrue
(
true
);
@DisplayName
(
"任务完成情况"
)
public
void
xxRwwcqkTest
()
{
String
secretKey
=
this
.
appStoreService
.
getAppSecretByAppKey
(
appId
);
Assertions
.
assertNotNull
(
secretKey
,
"appId不存在"
);
//断言appId存在,为空直接抛出异常不进行下一步测试,提高测试效率
// Assertions.fail(secretKey);
//请求参数
XxRwwcqkDTO
xxRwwcqkDTO
=
new
XxRwwcqkDTO
();
xxRwwcqkDTO
.
setRwid
(
"123456"
);
xxRwwcqkDTO
.
setXxyid
(
"123456"
);
xxRwwcqkDTO
.
setShrid
(
"123456"
);
xxRwwcqkDTO
.
setWczt
(
"1"
);
xxRwwcqkDTO
.
setBmzt
(
"1"
);
String
json
=
null
;
try
{
json
=
objectMapper
.
writeValueAsString
(
xxRwwcqkDTO
);
}
catch
(
JsonProcessingException
e
)
{
Assertions
.
fail
(
"json序列化失败"
);
}
//请求体加密
json
=
EncryptUtil
.
getInstance
().
AESEncode
(
json
,
secretKey
);
//签名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/index/addXxRwwcqk"
)
.
header
(
SignatureUtil
.
APPID
,
appId
)
.
header
(
SignatureUtil
.
NONCE
,
nonce
)
.
header
(
SignatureUtil
.
TIMESTAMP
,
timestampStr
)
.
header
(
SignatureUtil
.
SIGNATURE
,
generatedSignature
)
.
body
(
Mono
.
just
(
xxRwwcqkDTO
),
XxRwwcqkDTO
.
class
)
.
retrieve
()
.
bodyToMono
(
String
.
class
)
.
block
();
System
.
out
.
println
(
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