0

I have a microservce architecture with several services build using JHipster.

Inside one service i have implemented a zuul route filter.

public class TestZuulFilter extends ZuulFilter {

  @Override
  public String filterType() {
      return "route";
  }

  @Override
  public int filterOrder() {
    return 5;
  }

  @Override
  public boolean shouldFilter() {
      String requestUri = RequestContext.getCurrentContext().getRequest().getRequestURI();
      return "/serviceid/reverseproxy".equals(requestUri);
  }

  @Override
  public Object run() {

       // get url from id
       String id = ctx.getRequest().getParameter("id"); 
       Strign url = URLService.getURLFromId(id);

       try 
       {
          RequestContext ctx = RequestContext.getCurrentContext();

          // redirect
          ctx.setRouteHost(new URL(url));

       } catch(MalformedURLException ex) {}

       return null;
  }
}

When a client call my service http://myservice/serviceid/reverseproxy?id=2 zuul redirects (http 302 status) the user to the url with id 2, in this case google.com.

How can i preserve the original request URL from the client ?

The url must remain http://myservice/serviceid/reverseproxy?url=2 instead of http://www.google.com

Thanks in advance.

DropTheCode
  • 465
  • 1
  • 7
  • 14

1 Answers1

1

It seems you misunderstood the concepts of redirection and proxification.

HTTP redirection means URL change because all the work is done by the client who ends up by making 2 request calls (one to your proxy and one to external service).

Here what you want is to proxify the original request to an external service (in your example google), it means that your filter should be a client of your external service. This way your original client makes only on request call and has no idea that it is talking to your external service.

Gaël Marziou
  • 16,028
  • 4
  • 38
  • 49
  • Thank you for the explanation. Is possible to do the proxyfication with zuul ? Or i have to use another tool like nginx ? – DropTheCode Sep 30 '16 at 09:04
  • Zuul is a proxy, so yes it's possible.The difference with ngninx is that in Zuul, you can build your own business logic in java. So if your proxification rules are simple, ngninx will do the job. – Gaël Marziou Sep 30 '16 at 12:07
  • Let me recap. I have to create a route filter, inside the run method create an HttpClient that gets the content of the "external" URL ? Can you provide me an example ? – DropTheCode Sep 30 '16 at 12:31
  • RIght, how you implement your filter will highly depend on what kind of external service you target. – Gaël Marziou Sep 30 '16 at 12:45
  • The external services will be web applications like google.com or facebook.com (just examples). Create a filter to manage this kinda of resources is complicated and it is better to use nginx. Am i wrong ? – DropTheCode Sep 30 '16 at 13:00
  • Proxifying a web application is always complex and sometimes impossible because of links, domains, etc... Your context is too vague. If I was you, I would try to build a proof of concept without any programming using nginx, this way you'll be able to evaluate feasibility. – Gaël Marziou Sep 30 '16 at 16:12
  • I will use nginx because is much easier. Thank you for your help.Really appreciated – DropTheCode Oct 04 '16 at 07:28