Commit 86e5d1b2 authored by 以墨为白's avatar 以墨为白 🎧

测试服务

parent b1ab4f43
......@@ -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>
......
......@@ -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;
......
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);
}
}
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