From c68f071847940b478c2d8a1407de74638fc42f63 Mon Sep 17 00:00:00 2001 From: tanghc Date: Tue, 3 Sep 2019 16:03:50 +0800 Subject: [PATCH] 2.0 --- .../GatewayModifyResponseGatewayFilter.java | 10 +++-- .../gateway/filter/LimitFilter.java | 2 +- .../zuul/RequestContextUtil.java | 37 +++++++++++++++++++ .../zuul/filter/PostResultFilter.java | 11 ++---- .../java/com/gitee/sop/test/DownloadTest.java | 2 +- 5 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/RequestContextUtil.java diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/GatewayModifyResponseGatewayFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/GatewayModifyResponseGatewayFilter.java index ca3d5a9e..974a3a4c 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/GatewayModifyResponseGatewayFilter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/GatewayModifyResponseGatewayFilter.java @@ -2,6 +2,7 @@ package com.gitee.sop.gatewaycommon.gateway.filter; import com.gitee.sop.gatewaycommon.bean.ApiContext; import com.gitee.sop.gatewaycommon.result.ResultExecutor; +import org.apache.commons.lang3.StringUtils; import org.reactivestreams.Publisher; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; @@ -13,6 +14,7 @@ import org.springframework.core.Ordered; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseCookie; import org.springframework.http.client.reactive.ClientHttpResponse; import org.springframework.http.server.reactive.ServerHttpResponseDecorator; @@ -41,11 +43,13 @@ public class GatewayModifyResponseGatewayFilter implements GlobalFilter, Ordered @Override public Mono writeWith(Publisher body) { - + String originalResponseContentType = exchange.getAttribute(ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR); + // 如果是下载文件,直接放行,不合并结果 + if (StringUtils.containsIgnoreCase(originalResponseContentType, MediaType.APPLICATION_OCTET_STREAM_VALUE)) { + return chain.filter(exchange); + } Class inClass = String.class; Class outClass = String.class; - - String originalResponseContentType = exchange.getAttribute(ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR); HttpHeaders httpHeaders = new HttpHeaders(); //explicitly add it in this way instead of 'httpHeaders.setContentType(originalResponseContentType)' //this will prevent exception in case of using non-standard media types like "Content-Type: image" diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/LimitFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/LimitFilter.java index 8d5226d1..4c7e2a1e 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/LimitFilter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/LimitFilter.java @@ -37,7 +37,7 @@ public class LimitFilter implements GlobalFilter, Ordered { ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange); ConfigLimitDto configLimitDto = this.findConfigLimitDto(apiConfig, apiParam, exchange); if (configLimitDto == null) { - return null; + return chain.filter(exchange); } // 单个限流功能未开启 if (configLimitDto.getLimitStatus() == ConfigLimitDto.LIMIT_STATUS_CLOSE) { diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/RequestContextUtil.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/RequestContextUtil.java new file mode 100644 index 00000000..bea76c79 --- /dev/null +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/RequestContextUtil.java @@ -0,0 +1,37 @@ +package com.gitee.sop.gatewaycommon.zuul; + +import com.netflix.util.Pair; +import com.netflix.zuul.context.RequestContext; +import org.apache.commons.lang3.StringUtils; +import org.springframework.http.HttpHeaders; + +import java.util.List; + +/** + * @author tanghc + */ +public class RequestContextUtil { + + /** + * 获取微服务端传递过来的header + * + * @param name header名 + * @return 返回value,没有返回null + */ + public static String getZuulResponseHeader(RequestContext requestContext, String name) { + List> zuulResponseHeaders = requestContext.getZuulResponseHeaders(); + return zuulResponseHeaders.stream() + .filter(pair -> StringUtils.containsIgnoreCase(pair.first(), name)) + .findFirst() + .map(Pair::second) + .orElse(null); + } + + /** + * 获取微服务端的content-type + * @return 返回content-type + */ + public static String getZuulContentType(RequestContext requestContext) { + return getZuulResponseHeader(requestContext, HttpHeaders.CONTENT_TYPE); + } +} diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PostResultFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PostResultFilter.java index fabaffac..e5e920d9 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PostResultFilter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PostResultFilter.java @@ -4,7 +4,7 @@ import com.gitee.sop.gatewaycommon.bean.ApiConfig; import com.gitee.sop.gatewaycommon.bean.ApiContext; import com.gitee.sop.gatewaycommon.bean.SopConstants; import com.gitee.sop.gatewaycommon.result.ResultExecutor; -import com.netflix.util.Pair; +import com.gitee.sop.gatewaycommon.zuul.RequestContextUtil; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; import org.apache.commons.io.IOUtils; @@ -14,7 +14,6 @@ import org.springframework.http.MediaType; import javax.servlet.http.HttpServletResponse; import java.io.InputStream; -import java.util.List; /** * 合并微服务结果,统一返回格式 @@ -39,12 +38,9 @@ public class PostResultFilter extends BaseZuulFilter { if (response.isCommitted()) { return null; } - List> zuulResponseHeaders = requestContext.getZuulResponseHeaders(); - boolean isDownloadRequest = zuulResponseHeaders - .stream() - .anyMatch(pair -> StringUtils.contains(pair.second(), MediaType.APPLICATION_OCTET_STREAM_VALUE)); + String contentType = RequestContextUtil.getZuulContentType(requestContext); // 如果是文件下载直接返回 - if (isDownloadRequest) { + if (StringUtils.containsIgnoreCase(contentType, MediaType.APPLICATION_OCTET_STREAM_VALUE)) { return null; } InputStream responseDataStream = requestContext.getResponseDataStream(); @@ -62,4 +58,5 @@ public class PostResultFilter extends BaseZuulFilter { return null; } + } diff --git a/sop-test/src/test/java/com/gitee/sop/test/DownloadTest.java b/sop-test/src/test/java/com/gitee/sop/test/DownloadTest.java index 3bc242cb..170f4f81 100644 --- a/sop-test/src/test/java/com/gitee/sop/test/DownloadTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/DownloadTest.java @@ -19,7 +19,7 @@ import java.util.Map; */ public class DownloadTest extends TestBase { - String url = "http://localhost:8081/api"; // zuul + String url = "http://localhost:8081"; String appId = "2019032617262200001"; // 支付宝私钥 String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJdaQvoi06lgccmL+PHzminkFYON7alj1CjDN833j7QMHdPtS9l7B67fOU/p2LAAkPMtoVBfxQt9aFj7B8rEhGCz02iJIBAgMBAAECggEARqOuIpY0v6WtJBfmR3lGIOOokLrhfJrGTLF8CiZMQha+SRJ7/wOLPlsH9SbjPlopyViTXCuYwbzn2tdABigkBHYXxpDV6CJZjzmRZ+FY3S/0POlTFElGojYUJ3CooWiVfyUMhdg5vSuOq0oCny53woFrf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