fix:long转string

This commit is contained in:
waner
2026-05-06 16:52:41 +08:00
parent 855ea1247b
commit 11e6049ae2
10 changed files with 156 additions and 12 deletions
@@ -0,0 +1,18 @@
package com.cisd.tms.common.config;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JacksonLongSerializationConfig {
@Bean
public Jackson2ObjectMapperBuilderCustomizer longToStringJacksonCustomizer() {
return builder -> {
builder.serializerByType(Long.class, ToStringSerializer.instance);
builder.serializerByType(Long.TYPE, ToStringSerializer.instance);
};
}
}
@@ -74,6 +74,11 @@ public class GlobalExceptionHandler {
@ExceptionHandler(ReplayProtectionException.class)
public ResponseEntity<ApiResponse<Void>> handleReplayProtectionException(ReplayProtectionException ex, HttpServletRequest request) {
if (ex.isClientError()) {
return response(HttpStatus.BAD_REQUEST,
ApiResponse.fail(ErrorCode.BAD_REQUEST.getCode(), ex.getMessage())
.withPath(request.getRequestURI()));
}
log.warn("Replay protection 执行失败", ex);
return response(HttpStatus.SERVICE_UNAVAILABLE,
ApiResponse.fail(ErrorCode.SERVICE_UNAVAILABLE.getCode(), "replay protection unavailable")
@@ -2,11 +2,31 @@ package com.cisd.tms.modules.security.replay.exception;
public class ReplayProtectionException extends RuntimeException {
private final boolean clientError;
public ReplayProtectionException(String message) {
super(message);
this(message, false);
}
public ReplayProtectionException(String message, Throwable cause) {
this(message, cause, false);
}
private ReplayProtectionException(String message, boolean clientError) {
super(message);
this.clientError = clientError;
}
private ReplayProtectionException(String message, Throwable cause, boolean clientError) {
super(message, cause);
this.clientError = clientError;
}
public static ReplayProtectionException badRequest(String message) {
return new ReplayProtectionException(message, true);
}
public boolean isClientError() {
return clientError;
}
}
@@ -103,25 +103,25 @@ public class ReplayProtectionServiceImpl implements ReplayProtectionService {
private void validateRequest(ReplayCheckRequest request) {
if (request == null) {
throw new ReplayProtectionException("replay 请求不能为空");
throw ReplayProtectionException.badRequest("replay 请求不能为空");
}
if (request.getScope() == null) {
throw new ReplayProtectionException("replay scope不能为空");
throw ReplayProtectionException.badRequest("replay scope不能为空");
}
if (isBlank(request.getPrincipalId())) {
throw new ReplayProtectionException("principal id不能为空");
throw ReplayProtectionException.badRequest("principal id不能为空");
}
if (isBlank(request.getNonce())) {
throw new ReplayProtectionException("nonce不能为空");
throw ReplayProtectionException.badRequest("nonce不能为空");
}
if (request.getRequestTimestamp() <= 0L) {
throw new ReplayProtectionException("request timestamp不能为空");
throw ReplayProtectionException.badRequest("request timestamp不能为空");
}
}
private void ensureTimestampWithinWindow(long requestTimestamp, long nowSeconds, long allowedSkewSeconds) {
if (Math.abs(nowSeconds - requestTimestamp) > allowedSkewSeconds) {
throw new ReplayProtectionException("请求时间戳超出允许窗口");
throw ReplayProtectionException.badRequest("请求时间戳超出允许窗口");
}
}
@@ -78,8 +78,7 @@ public class InternalApiReplayInterceptor implements HandlerInterceptor {
return false;
}
} catch (ReplayProtectionException ex) {
writeJson(response, HttpServletResponse.SC_SERVICE_UNAVAILABLE, ErrorCode.SERVICE_UNAVAILABLE.getCode(),
"replay protection unavailable");
writeReplayProtectionError(response, ex);
return false;
}
@@ -147,4 +146,13 @@ public class InternalApiReplayInterceptor implements HandlerInterceptor {
private void writeJson(HttpServletResponse response, int status, int code, String message) throws IOException {
HttpResponseUtil.writeJson(response, status, ApiResponse.fail(code, message), objectMapper);
}
private void writeReplayProtectionError(HttpServletResponse response, ReplayProtectionException ex) throws IOException {
if (ex.isClientError()) {
writeJson(response, HttpServletResponse.SC_BAD_REQUEST, ErrorCode.BAD_REQUEST.getCode(), ex.getMessage());
return;
}
writeJson(response, HttpServletResponse.SC_SERVICE_UNAVAILABLE, ErrorCode.SERVICE_UNAVAILABLE.getCode(),
"replay protection unavailable");
}
}
@@ -97,8 +97,7 @@ public class OpenApiSignAuthInterceptor implements HandlerInterceptor {
return false;
}
} catch (ReplayProtectionException ex) {
writeJson(response, HttpServletResponse.SC_SERVICE_UNAVAILABLE,
ErrorCode.SERVICE_UNAVAILABLE.getCode(), "replay protection unavailable");
writeReplayProtectionError(response, ex);
return false;
}
}
@@ -153,4 +152,13 @@ public class OpenApiSignAuthInterceptor implements HandlerInterceptor {
objectMapper
);
}
private void writeReplayProtectionError(HttpServletResponse response, ReplayProtectionException ex) throws IOException {
if (ex.isClientError()) {
writeJson(response, HttpServletResponse.SC_BAD_REQUEST, ErrorCode.BAD_REQUEST.getCode(), ex.getMessage());
return;
}
writeJson(response, HttpServletResponse.SC_SERVICE_UNAVAILABLE,
ErrorCode.SERVICE_UNAVAILABLE.getCode(), "replay protection unavailable");
}
}