spring cloud gateway intercept all incoming and outgoing request

2.4k views Asked by At

It going to be one backend application that expose some REST endpoint and can make call to other backend. This application run in one host. I want on same host run other application - Sidecar. It should: 1)Sidecar should receive all outgoing traffic of Backend app to add additional auth header. 2)Sidecar should receive all incoming traffic and check auth header then forward to Backend app.

Sidecar should be spring-boot app. I going to choose -Spring cloud gateway.

To solve 1) I going to call all Backend App request by Proxy - and proxy is Sidecar cloud GW To solve 2) I going to expose only Sidecar endpoint /** that receive all incoming traffic and forward to Backend App

Next code work for When Backend App initiate outgoing request and use Sidecar as Proxy

@SpringBootApplication
@RestController
public class DemoApplication {

public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
}

@RequestMapping("/**")
public Mono<ResponseEntity<byte[]>> proxy(ProxyExchange<byte[]> proxy, ServerHttpRequest serverHttpRequest) throws Exception {
    return proxy.uri(serverHttpRequest.getURI().toString()).forward();
}
}

At same time code above will intercept all external incoming request and send it again to Sidecar that lead to infinite loop.

Any suggestion haw to write Sidecar(Spring cloud gateway) that from the one hand it will be used as Proxy for outgoing request and from the other hand Forward all incoming request to Backend App

Thanks

0

There are 0 answers