|
|
|
@ -1,6 +1,7 @@
@@ -1,6 +1,7 @@
|
|
|
|
|
package com.ds.gateway.filter; |
|
|
|
|
|
|
|
|
|
import com.ds.commons.utils.json.JsonUtil; |
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.reactivestreams.Publisher; |
|
|
|
@ -14,6 +15,7 @@ import org.springframework.http.HttpStatus;
@@ -14,6 +15,7 @@ import org.springframework.http.HttpStatus;
|
|
|
|
|
import org.springframework.http.server.reactive.ServerHttpRequest; |
|
|
|
|
import org.springframework.http.server.reactive.ServerHttpResponse; |
|
|
|
|
import org.springframework.http.server.reactive.ServerHttpResponseDecorator; |
|
|
|
|
import org.springframework.lang.NonNull; |
|
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
import org.springframework.web.server.ServerWebExchange; |
|
|
|
|
import reactor.core.publisher.Flux; |
|
|
|
@ -23,6 +25,10 @@ import java.nio.charset.StandardCharsets;
@@ -23,6 +25,10 @@ import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 因为swagger配置在业务服务中,默认的url会比网关少一层; |
|
|
|
|
* 这个过滤器主要给url添加上此网关这一层,并且设置到swagger的basePath中,使其可以正常通过swagger调用接口 |
|
|
|
|
*/ |
|
|
|
|
@Slf4j |
|
|
|
|
@Component |
|
|
|
|
public class SwaggerGlobalFilter implements GlobalFilter, Ordered { |
|
|
|
@ -30,20 +36,18 @@ public class SwaggerGlobalFilter implements GlobalFilter, Ordered {
@@ -30,20 +36,18 @@ public class SwaggerGlobalFilter implements GlobalFilter, Ordered {
|
|
|
|
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { |
|
|
|
|
ServerHttpRequest request = exchange.getRequest(); |
|
|
|
|
String path = request.getPath().toString(); |
|
|
|
|
String host = request.getLocalAddress().getHostString(); |
|
|
|
|
int port = request.getLocalAddress().getPort(); |
|
|
|
|
if (!path.endsWith("/v3/api-docs")) { |
|
|
|
|
if (!path.endsWith("/v3/api-docs/swagger-config")) { |
|
|
|
|
return chain.filter(exchange); |
|
|
|
|
} |
|
|
|
|
String[] pathArray = path.split("/"); |
|
|
|
|
System.out.println(pathArray); |
|
|
|
|
String basePath = pathArray[1]; |
|
|
|
|
ServerHttpResponse originalResponse = exchange.getResponse(); |
|
|
|
|
// 定义新的消息头
|
|
|
|
|
ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) { |
|
|
|
|
@Override |
|
|
|
|
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { |
|
|
|
|
if (super.getStatusCode().equals(HttpStatus.OK) && body instanceof Flux) { |
|
|
|
|
@NonNull |
|
|
|
|
public Mono<Void> writeWith(@NonNull Publisher<? extends DataBuffer> body) { |
|
|
|
|
if (HttpStatus.OK.equals(super.getStatusCode()) && body instanceof Flux) { |
|
|
|
|
Flux<? extends DataBuffer> fluxBody = Flux.from(body); |
|
|
|
|
return super.writeWith(fluxBody.buffer().map(dataBuffers -> { |
|
|
|
|
List<String> list = new ArrayList<>(); |
|
|
|
@ -54,9 +58,12 @@ public class SwaggerGlobalFilter implements GlobalFilter, Ordered {
@@ -54,9 +58,12 @@ public class SwaggerGlobalFilter implements GlobalFilter, Ordered {
|
|
|
|
|
list.add(new String(content, StandardCharsets.UTF_8)); |
|
|
|
|
}); |
|
|
|
|
String s = this.listToString(list); |
|
|
|
|
//获取到/v3/api-docs/swagger-config返回的json串 给文档url添加上网关层url
|
|
|
|
|
ObjectNode jsonNodes = JsonUtil.parseObject(s,ObjectNode.class); |
|
|
|
|
JsonNode urls = jsonNodes.get("urls"); |
|
|
|
|
urls.elements().forEachRemaining(item-> ((ObjectNode)item).put("url","/"+basePath+item.get("url").asText())); |
|
|
|
|
|
|
|
|
|
jsonNodes.put("host", host + ":" + port); |
|
|
|
|
//设置basePath
|
|
|
|
|
jsonNodes.put("basePath", basePath); |
|
|
|
|
s = jsonNodes.toString(); |
|
|
|
|
// 设置更新后的header请求头长度
|
|
|
|
@ -70,6 +77,7 @@ public class SwaggerGlobalFilter implements GlobalFilter, Ordered {
@@ -70,6 +77,7 @@ public class SwaggerGlobalFilter implements GlobalFilter, Ordered {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
@NonNull |
|
|
|
|
public HttpHeaders getHeaders() { |
|
|
|
|
// 获取父类原始ServerHttpResponse的header请求头信息,这是代理Delegate类型
|
|
|
|
|
HttpHeaders httpHeaders = super.getHeaders(); |
|
|
|
|