0

I am a noob in spring boot. I am writing a Gateway for some services. In a condition, I need to forward user request to other services (some Restful API) after authentication. I have done some search on 'forward' and 'redirect'. I think I need 'forward'. But I still have some questions: 1. when I forward it to other URI(eg. abc.dce.com/service/), does the service get the request body. 2.How can I do it in spring boot? Do you guys have a good example that fit my condition? (I admit that I am kind of lazy for this, but there are really many style of forward that confused me.)

//I find this example, but this is forwarding to service in same package //under same Internet. 
@Override
public void addViewControllers(ViewControllerRegistry registry) {
    // forward requests to /admin and /user to their index.html
    registry.addViewController("/portal").setViewName(
            "forward:/app/index.html");
}
DD Jin
  • 355
  • 1
  • 3
  • 15
  • Use HttpClient to reach other REST service. https://hc.apache.org/httpcomponents-client-4.5.x/quickstart.html – Anton N Jun 21 '19 at 17:02

1 Answers1

0

Since you mention you're new to spring boot, you might want to take a look at spring project that implements a fully-featured gateway. I've used an earlier version of it (zuul) and the current spring-cloud-gateway allows you to implement a complete gateway easily by creating a spring-boot project and configuring. It has a lot of features you'll likely want to implement as a gateway (like adding/removing headers, modifying payloads,..). If you need features they don't support, you can implement via filters and other interfaces they provide. This was initially opensourced from Netflix so it is fairly comprehensive.

https://spring.io/projects/spring-cloud-gateway

Sample project: https://github.com/spring-cloud-samples/spring-cloud-gateway-sample

John B
  • 66
  • 4
  • Thanks. I am going with Filter. I just want to redirect request header and body to another restful service on other server. Then get the response and give it back to front end. I find the method here (don't know if it work): https://stackoverflow.com/questions/14726082/spring-mvc-rest-service-redirect-forward-proxy/23736527 – DD Jin Jun 21 '19 at 23:16