logger
parent
ddd424bbf0
commit
96e950cb00
|
|
@ -44,11 +44,11 @@ public final class DefaultValidator implements Validator {
|
||||||
Instant responseTime = Instant.ofEpochSecond(Long.parseLong(timestamp));
|
Instant responseTime = Instant.ofEpochSecond(Long.parseLong(timestamp));
|
||||||
// 拒绝过期请求
|
// 拒绝过期请求
|
||||||
if (Duration.between(responseTime, Instant.now()).abs().toMinutes() >= RESPONSE_EXPIRED_MINUTES) {
|
if (Duration.between(responseTime, Instant.now()).abs().toMinutes() >= RESPONSE_EXPIRED_MINUTES) {
|
||||||
throw new TimestampException(String.format("响应校验失败, 响应头[%s=%s]已过期, Request-Id[%s]", TIMESTAMP,
|
throw new TimestampException(String.format("响应校验失败, 响应头[%s=%s]已过期, Request-Id=%s", TIMESTAMP,
|
||||||
timestamp, response.getHttpHeaders().getHeader(REQUEST_ID)));
|
timestamp, response.getHttpHeaders().getHeader(REQUEST_ID)));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new TimestampException(String.format("响应校验失败, 响应头[%s=%s]无效, Request-Id[%s]", TIMESTAMP,
|
throw new TimestampException(String.format("响应校验失败, 响应头[%s=%s]无效, Request-Id=%s", TIMESTAMP,
|
||||||
timestamp, response.getHttpHeaders().getHeader(REQUEST_ID)));
|
timestamp, response.getHttpHeaders().getHeader(REQUEST_ID)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -73,11 +73,12 @@ public final class DefaultValidator implements Validator {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!verifier.verify(null, message, signature)) {
|
if (!verifier.verify(null, message, signature)) {
|
||||||
throw new ValidationException(String.format("响应校验失败, 签名校验未通过, Request-Id[%s]",
|
httpLogger.logResponseError(response);
|
||||||
|
throw new ValidationException(String.format("响应校验失败, 签名校验未通过, Request-Id=%s",
|
||||||
response.getHttpHeaders().getHeader(REQUEST_ID)));
|
response.getHttpHeaders().getHeader(REQUEST_ID)));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ValidationException(String.format("响应校验失败, 签名校验异常, Request-Id[%s]",
|
throw new ValidationException(String.format("响应校验失败, 签名校验异常, Request-Id=%s",
|
||||||
response.getHttpHeaders().getHeader(REQUEST_ID)), e);
|
response.getHttpHeaders().getHeader(REQUEST_ID)), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,10 @@ public class HttpLogger {
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("请求行:{} {} {}", request.getHttpMethod(), request.getUrl(), httpVersion);
|
logger.info("请求行:{} {} {}", request.getHttpMethod(), request.getUrl(), httpVersion);
|
||||||
request.getHttpHeaders().getHeaders().forEach((k, v) -> logger.info("请求头:{}={}", k, v));
|
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
request.getHttpHeaders().getHeaders().forEach((k, v) -> sb.append(k).append("=").append(v).append(";"));
|
||||||
|
logger.info("请求头:{}", Strings.removeSuffix(sb.toString(), ";"));
|
||||||
|
|
||||||
if (request.getRequestBody() instanceof EncryptRequestBody) {
|
if (request.getRequestBody() instanceof EncryptRequestBody) {
|
||||||
EncryptRequestBody body = (EncryptRequestBody) request.getRequestBody();
|
EncryptRequestBody body = (EncryptRequestBody) request.getRequestBody();
|
||||||
|
|
@ -55,7 +58,9 @@ public class HttpLogger {
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("响应行:{} {}", originalResponse.getVersion(), originalResponse.getStatusCode());
|
logger.info("响应行:{} {}", originalResponse.getVersion(), originalResponse.getStatusCode());
|
||||||
response.getHttpHeaders().getHeaders().forEach((k, v) -> logger.info("响应头:{}={}", k, v));
|
StringBuilder sb = new StringBuilder();
|
||||||
|
response.getHttpHeaders().getHeaders().forEach((k, v) -> sb.append(k).append("=").append(v).append(";"));
|
||||||
|
logger.info("响应头:{}", Strings.removeSuffix(sb.toString(), ";"));
|
||||||
logger.info("响应原始报文:{}", Strings.toStr(response.getOriginalBody()));
|
logger.info("响应原始报文:{}", Strings.toStr(response.getOriginalBody()));
|
||||||
logger.info("响应解密报文:{}", Json.toJson(response.getServiceResponse()));
|
logger.info("响应解密报文:{}", Json.toJson(response.getServiceResponse()));
|
||||||
|
|
||||||
|
|
@ -69,7 +74,9 @@ public class HttpLogger {
|
||||||
*/
|
*/
|
||||||
public void logResponseError(OriginalResponse originalResponse) {
|
public void logResponseError(OriginalResponse originalResponse) {
|
||||||
logger.info("响应:{} {}", originalResponse.getVersion(), originalResponse.getStatusCode());
|
logger.info("响应:{} {}", originalResponse.getVersion(), originalResponse.getStatusCode());
|
||||||
originalResponse.getHttpHeaders().getHeaders().forEach((k, v) -> logger.info("响应头:{}={}", k, v));
|
StringBuilder sb = new StringBuilder();
|
||||||
|
originalResponse.getHttpHeaders().getHeaders().forEach((k, v) -> sb.append(k).append("=").append(v).append(";"));
|
||||||
|
logger.info("响应头:{}", Strings.removeSuffix(sb.toString(), ";"));
|
||||||
logger.info("响应原始报文:{}", Strings.toStr((originalResponse.getBody())));
|
logger.info("响应原始报文:{}", Strings.toStr((originalResponse.getBody())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,14 @@ public class Strings {
|
||||||
return new String(bytes, StandardCharsets.UTF_8);
|
return new String(bytes, StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String removeSuffix(String text, String suffix) {
|
||||||
|
if (isEmpty(text) || isEmpty(suffix)) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return text.substring(0, text.length() - suffix.length());
|
||||||
|
}
|
||||||
|
|
||||||
public static byte[] toBytes(String text) {
|
public static byte[] toBytes(String text) {
|
||||||
return text.getBytes(StandardCharsets.UTF_8);
|
return text.getBytes(StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,4 +61,13 @@ class StringsTest {
|
||||||
Assertions.assertEquals("https%3A%2F%2Fwww.baidu.com%2Fs%3Fie%3Dutf-8", Strings.urlEncode(url));
|
Assertions.assertEquals("https%3A%2F%2Fwww.baidu.com%2Fs%3Fie%3Dutf-8", Strings.urlEncode(url));
|
||||||
assertThrows(EncodingException.class, () -> Strings.urlEncode(null));
|
assertThrows(EncodingException.class, () -> Strings.urlEncode(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void removeSuffix() {
|
||||||
|
Assertions.assertEquals("", Strings.removeSuffix("", ""));
|
||||||
|
Assertions.assertEquals("", Strings.removeSuffix(null, ""));
|
||||||
|
Assertions.assertEquals("", Strings.removeSuffix("", null));
|
||||||
|
Assertions.assertEquals("123", Strings.removeSuffix("123;", ";"));
|
||||||
|
Assertions.assertEquals("", Strings.removeSuffix("123;", null));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue