SpringCloudGateWay path modification request, access to services from the registry

SpringCloudGateWay path modification request, access to services from the registry

    @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( System.out.println (newRequest.getHeaders ());"POST the redirect URL {}" , URL); System.out.println ( "POST the redirect URL {}" + URL); // postBody = exchange.getAttribute ( "cachedRequestBodyObject"); // will again write request for encapsulating Back in the request, transmitted to the next stage. the URI ETX = URI.create (URL); // modified request path ServerHTTPRequest NewRequest = . req.mutate () URI (ETX) .build (); the DataBuffer bodyDataBuffer = stringBuffer (postBody); Flux <the 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); }

 

Guess you like

Origin www.cnblogs.com/saozhou/p/11525422.html