SpringCloudGateWay修改请求路径,从注册中心获得服务

SpringCloudGateWay修改请求路径,从注册中心获得服务

    @Resource
    private DiscoveryClient disClient;
    @Resource
    private LoadBalancerClient loadBlance;
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { String contentType; ServiceInstance serviceInstance = null; boolean formdataType = false; String postBody = ""; String ip = ""; String tokenID = ""; ServerHttpRequest req = exchange.getRequest(); ServerHttpResponse rep = exchange.getResponse(); String path = req.getURI().getPath(); String port = Integer.toString(req.getURI().getPort()); URI requestUri = req.getURI(); String method = req.getMethod().name(); String schema = requestUri.getScheme(); if ((!"http".equals(schema) && !"https".equals(schema))) { return chain.filter(exchange); } if (method.equals("GET") && port.equals(localPort)) { return chain.filter(exchange); } else if(method.equals("POST") && (path.equals("/token")||path.equals("/callMESFunctionNoAuth"))) { chain.filter(exchange); } else if (method.equals("POST") && port.equals(localPort)) { AccessRecord accessRecord = new AccessRecord(); accessRecord.setPath(requestUri.getPath()); accessRecord.setQueryString(req.getQueryParams()); exchange.getAttributes().put("startTime", System.currentTimeMillis()); ip = exchange.getRequest().getRemoteAddress().getAddress().getHostAddress(); try { postBody = resolveBodyFromRequest(req);if (true) { contentType = exchange.getRequest().getHeaders().getFirst("Content-Type"); if (!StringUtil.isEmpty(contentType)) { formdataType = contentType.startsWith("multipart/form-data"); } if (!formdataType) { URL uri = service.getURI(auth.getFunCode()); String url = null; if (uri != null) { try { serviceInstance = loadBlance.choose(uri.getApplicationName()); } catch (Exception e) { e.printStackTrace(); } url = schema + "://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + uri.getUrl(); } else { url = schema + "://" + mesUrl + requestUri.getPath(); } log.info("post redirect url {} ", url); System.out.println("post redirect url {} " + url); // postBody = exchange.getAttribute("cachedRequestBodyObject"); // 下面将请求体再次封装写回到 request 里,传到下一级. URI eTx = URI.create(url);//修改了请求的路径 ServerHttpRequest newRequest = req.mutate().uri(eTx).build(); System.out.println(newRequest.getHeaders()); DataBuffer bodyDataBuffer = stringBuffer(postBody); Flux<DataBuffer> bodyFlux = Flux.just(bodyDataBuffer); newRequest = new ServerHttpRequestDecorator(newRequest) { @Override public Flux<DataBuffer> getBody() { return bodyFlux; } }; accessRecord.setBody(formatStr(postBody)); ServerWebExchange newExchange = exchange.mutate().request(newRequest).build(); exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, newRequest.getURI()); exchange = newExchange; } return returnMono(chain, exchange, accessRecord); } else { rep.setStatusCode(HttpStatus.FORBIDDEN); return rep.setComplete(); } } catch (Exception e) { log.error("IP:{},URI:{},TokenID:{},Exception:{}", ip, path, tokenID, e); rep.setStatusCode(HttpStatus.FORBIDDEN); return rep.setComplete(); } } return chain.filter(exchange); }

 

posted @ 2019-09-16 08:34  saozhou  阅读(7636)  评论(0编辑  收藏  举报