Don't encrypt error response

This commit is contained in:
hongqiaowei
2021-11-29 16:20:35 +08:00
parent 7afc834671
commit 3214ccad6b
5 changed files with 36 additions and 12 deletions

View File

@@ -27,6 +27,11 @@ public final class Consts {
} }
public static final class S { public static final class S {
public static final String TRUE = "true";
public static final String FALSE = "false";
public static final String TRUE1 = "1";
public static final String FALSE0 = "0";
public static final String EMPTY = ""; public static final String EMPTY = "";
public static final String SPACE_STR = " "; public static final String SPACE_STR = " ";
public static final char SPACE = ' '; public static final char SPACE = ' ';

View File

@@ -148,15 +148,20 @@ class DedicatedLineHttpHandler implements HttpHandler {
log.debug(sb.toString()); log.debug(sb.toString());
} }
Flux<DataBuffer> remoteRespBody = remoteResp.body(BodyExtractors.toDataBuffers()); Flux<DataBuffer> remoteRespBody = remoteResp.body(BodyExtractors.toDataBuffers());
if (systemConfig.fizzDedicatedLineClientRequestCrypto()) { if (systemConfig.fizzDedicatedLineClientRequestCrypto()) {
if (response.getStatusCode() == HttpStatus.OK) {
String v = respHeaders.getFirst(WebUtils.BODY_ENCRYPT);
if (org.apache.commons.lang3.StringUtils.isBlank(v) || v.equals(Consts.S.TRUE1)) {
respHeaders.remove(HttpHeaders.CONTENT_LENGTH); respHeaders.remove(HttpHeaders.CONTENT_LENGTH);
return response.writeWith (decrypt(remoteRespBody, dedicatedLineInfo.requestCryptoKey)); return response.writeWith (decrypt(remoteRespBody, dedicatedLineInfo.requestCryptoKey));
} else { }
}
}
return response.writeWith (remoteRespBody) return response.writeWith (remoteRespBody)
.doOnError ( throwable -> cleanup(remoteResp) ) .doOnError ( throwable -> cleanup(remoteResp) )
.doOnCancel( () -> cleanup(remoteResp) ); .doOnCancel( () -> cleanup(remoteResp) );
} }
}
); );
return respMono.doOnSuccess ( v -> logResponse(exchange) ) return respMono.doOnSuccess ( v -> logResponse(exchange) )

View File

@@ -17,6 +17,7 @@
package we.filter; package we.filter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@@ -30,6 +31,7 @@ import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebExceptionHandler; import org.springframework.web.server.WebExceptionHandler;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import we.Fizz;
import we.config.SystemConfig; import we.config.SystemConfig;
import we.exception.ExecuteScriptException; import we.exception.ExecuteScriptException;
import we.exception.RedirectException; import we.exception.RedirectException;
@@ -37,6 +39,7 @@ import we.exception.StopAndResponseException;
import we.fizz.exception.FizzRuntimeException; import we.fizz.exception.FizzRuntimeException;
import we.flume.clients.log4j2appender.LogService; import we.flume.clients.log4j2appender.LogService;
import we.legacy.RespEntity; import we.legacy.RespEntity;
import we.util.Consts;
import we.util.JacksonUtils; import we.util.JacksonUtils;
import we.util.ThreadContext; import we.util.ThreadContext;
import we.util.WebUtils; import we.util.WebUtils;
@@ -67,10 +70,16 @@ public class FilterExceptionHandlerConfig {
} }
} }
HttpHeaders respHeaders = resp.getHeaders();
String value = Fizz.context.getEnvironment().getProperty(SystemConfig.FIZZ_DEDICATED_LINE_CLIENT_ENABLE);
if (StringUtils.isNotBlank(value) && value.equals(Consts.S.TRUE)) {
respHeaders.set(WebUtils.BODY_ENCRYPT, Consts.S.FALSE0);
}
if (t instanceof StopAndResponseException) { if (t instanceof StopAndResponseException) {
StopAndResponseException ex = (StopAndResponseException) t; StopAndResponseException ex = (StopAndResponseException) t;
if (ex.getData() != null) { if (ex.getData() != null) {
resp.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); respHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
return resp.writeWith(Mono.just(resp.bufferFactory().wrap(ex.getData().toString().getBytes()))); return resp.writeWith(Mono.just(resp.bufferFactory().wrap(ex.getData().toString().getBytes())));
} }
} }
@@ -78,7 +87,7 @@ public class FilterExceptionHandlerConfig {
RedirectException ex = (RedirectException) t; RedirectException ex = (RedirectException) t;
if (ex.getRedirectUrl() != null) { if (ex.getRedirectUrl() != null) {
resp.setStatusCode(HttpStatus.MOVED_PERMANENTLY); resp.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
resp.getHeaders().setLocation(URI.create(ex.getRedirectUrl())); respHeaders.setLocation(URI.create(ex.getRedirectUrl()));
return Mono.empty(); return Mono.empty();
} }
} }
@@ -89,7 +98,7 @@ public class FilterExceptionHandlerConfig {
} }
if (t instanceof ExecuteScriptException) { if (t instanceof ExecuteScriptException) {
ExecuteScriptException ex = (ExecuteScriptException) t; ExecuteScriptException ex = (ExecuteScriptException) t;
resp.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); respHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
RespEntity rs = null; RespEntity rs = null;
if (ex.getStepContext() != null && ex.getStepContext().returnContext()) { if (ex.getStepContext() != null && ex.getStepContext().returnContext()) {
rs = new RespEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, traceId, ex.getStepContext()); rs = new RespEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, traceId, ex.getStepContext());
@@ -103,7 +112,7 @@ public class FilterExceptionHandlerConfig {
if (t instanceof FizzRuntimeException) { if (t instanceof FizzRuntimeException) {
FizzRuntimeException ex = (FizzRuntimeException) t; FizzRuntimeException ex = (FizzRuntimeException) t;
log.error(traceId + ' ' + tMsg, LogService.BIZ_ID, traceId, ex); log.error(traceId + ' ' + tMsg, LogService.BIZ_ID, traceId, ex);
resp.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); respHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
RespEntity rs = null; RespEntity rs = null;
if (ex.getStepContext() != null && ex.getStepContext().returnContext()) { if (ex.getStepContext() != null && ex.getStepContext().returnContext()) {
rs = new RespEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, traceId, ex.getStepContext()); rs = new RespEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, traceId, ex.getStepContext());
@@ -121,7 +130,7 @@ public class FilterExceptionHandlerConfig {
WebUtils.request2stringBuilder(exchange, b); WebUtils.request2stringBuilder(exchange, b);
log.error(b.toString(), LogService.BIZ_ID, traceId, t); log.error(b.toString(), LogService.BIZ_ID, traceId, t);
String s = WebUtils.jsonRespBody(HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, traceId); String s = WebUtils.jsonRespBody(HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, traceId);
resp.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); respHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
vm = resp.writeWith(Mono.just(resp.bufferFactory().wrap(s.getBytes()))); vm = resp.writeWith(Mono.just(resp.bufferFactory().wrap(s.getBytes())));
} else { } else {
vm = WebUtils.responseError(exchange, filterExceptionHandler, HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, t); vm = WebUtils.responseError(exchange, filterExceptionHandler, HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, t);

View File

@@ -113,6 +113,8 @@ public abstract class WebUtils {
public static final String FIZZ_REQUEST = "fr@"; public static final String FIZZ_REQUEST = "fr@";
public static final String BODY_ENCRYPT = "b-ecyt";
private WebUtils() { private WebUtils() {
} }
@@ -489,6 +491,9 @@ public abstract class WebUtils {
// } // }
// ); // );
// } // }
exchange.getResponse().getHeaders().set(BODY_ENCRYPT, Consts.S.FALSE0);
String traceId = getTraceId(exchange); String traceId = getTraceId(exchange);
// Schedulers.parallel().schedule(() -> { // Schedulers.parallel().schedule(() -> {
StringBuilder b = ThreadContext.getStringBuilder(); StringBuilder b = ThreadContext.getStringBuilder();

View File

@@ -87,7 +87,7 @@ public class DedicatedLineCodecPluginFilter extends RequestBodyPlugin {
FizzServerHttpResponseDecorator fizzServerHttpResponseDecorator = new FizzServerHttpResponseDecorator(original) { FizzServerHttpResponseDecorator fizzServerHttpResponseDecorator = new FizzServerHttpResponseDecorator(original) {
@Override @Override
public Publisher<? extends DataBuffer> writeWith(DataBuffer remoteResponseBody) { public Publisher<? extends DataBuffer> writeWith(DataBuffer remoteResponseBody) {
if (remoteResponseBody == NettyDataBufferUtils.EMPTY_DATA_BUFFER) { if (remoteResponseBody == null || remoteResponseBody == NettyDataBufferUtils.EMPTY_DATA_BUFFER) {
return Mono.empty(); return Mono.empty();
} else { } else {
if (StringUtils.isNotBlank(cryptoKey)) { if (StringUtils.isNotBlank(cryptoKey)) {