|
@@ -2,17 +2,26 @@ package com.diagbot.filter;
|
|
|
|
|
|
import com.diagbot.client.DiagbotmanServiceClient;
|
|
import com.diagbot.client.DiagbotmanServiceClient;
|
|
import com.diagbot.client.UserServiceClient;
|
|
import com.diagbot.client.UserServiceClient;
|
|
|
|
+import com.diagbot.dto.RespDTO;
|
|
|
|
+import com.diagbot.exception.CommonErrorCode;
|
|
|
|
+import com.diagbot.exception.CommonException;
|
|
|
|
+import com.diagbot.exception.ErrorCode;
|
|
|
|
+import com.diagbot.util.GsonUtil;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
|
|
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
+import org.springframework.core.io.buffer.DataBuffer;
|
|
|
|
+import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
|
|
+import reactor.core.publisher.Flux;
|
|
import reactor.core.publisher.Mono;
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
import java.net.URI;
|
|
import java.net.URI;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.Iterator;
|
|
import java.util.LinkedHashSet;
|
|
import java.util.LinkedHashSet;
|
|
@@ -69,6 +78,10 @@ public class GlobalGatewayFilter implements GlobalFilter {
|
|
// }
|
|
// }
|
|
// if(1 == 1) {
|
|
// if(1 == 1) {
|
|
// throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "出错了!");
|
|
// throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "出错了!");
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+// if(1==1){
|
|
|
|
+// return getVoidMono(serverWebExchange, CommonErrorCode.NO_PERMISSION);
|
|
// }
|
|
// }
|
|
|
|
|
|
ServerHttpRequest.Builder builder = serverWebExchange.getRequest().mutate();
|
|
ServerHttpRequest.Builder builder = serverWebExchange.getRequest().mutate();
|
|
@@ -76,4 +89,20 @@ public class GlobalGatewayFilter implements GlobalFilter {
|
|
gatewayFilterChain.filter(serverWebExchange.mutate().request(builder.build()).build());
|
|
gatewayFilterChain.filter(serverWebExchange.mutate().request(builder.build()).build());
|
|
return gatewayFilterChain.filter(serverWebExchange.mutate().request(builder.build()).build());
|
|
return gatewayFilterChain.filter(serverWebExchange.mutate().request(builder.build()).build());
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 网关抛异常
|
|
|
|
+ *
|
|
|
|
+ * @param body
|
|
|
|
+ */
|
|
|
|
+ private Mono<Void> getVoidMono(ServerWebExchange serverWebExchange, ErrorCode body) {
|
|
|
|
+ RespDTO resp = new RespDTO();
|
|
|
|
+ serverWebExchange.getResponse().setStatusCode(HttpStatus.OK);
|
|
|
|
+ resp.msg = body.getMsg();
|
|
|
|
+ resp.code = body.getCode();
|
|
|
|
+ resp.data = "";
|
|
|
|
+ byte[] bytes = GsonUtil.toJson(resp).getBytes(StandardCharsets.UTF_8);
|
|
|
|
+ DataBuffer buffer = serverWebExchange.getResponse().bufferFactory().wrap(bytes);
|
|
|
|
+ return serverWebExchange.getResponse().writeWith(Flux.just(buffer));
|
|
|
|
+ }
|
|
}
|
|
}
|