htshame
htshame

Reputation: 7340

Is there a way to use Zuul to proxy inner http calls?

I have a spring-boot app, that I can make an HTTP request to, and which will send another HTTP request to some other resource on the internet.

@RestController
@SpringBootApplication
public class BookApplication {

  @RequestMapping(value = "/available")
  public String available() throws Exception {

    String url = "https://www.google.com";

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    con.setRequestMethod("GET");
    con.setRequestProperty("User-Agent", "Mozilla/5.0");
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    StringBuffer response = new StringBuffer();
    String inputLine;

    while ((inputLine = in.readLine()) != null) {
      response.append(inputLine);
    }
    in.close();
    return "Spring in Action";
  }

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

And I also have another spring-boot app that is a Zuul proxy.

@EnableZuulProxy
@SpringBootApplication
public class GatewayApplication {

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

  @Bean
  public SimpleFilter simpleFilter() {
    return new SimpleFilter();
  }
}

SimpleFilter class is:

public class SimpleFilter extends ZuulFilter {

  private static Logger log = LoggerFactory.getLogger(SimpleFilter.class);

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

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

  @Override
  public boolean shouldFilter() {
    return true;
  }

  @Override
  public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();

    log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));

    return null;
  }

}

And application.properties for Zuul proxy app:

zuul.routes.books.url=http://localhost:8090

ribbon.eureka.enabled=false

server.port=8080

Basically, everything is from this tutorial tutorial

So I'm wondering if there is a chance to proxy the request to "https://www.google.com" that's done by /available resource in the BookApplication?

Upvotes: 0

Views: 788

Answers (2)

r_benedikt
r_benedikt

Reputation: 54

you can overwrite the standard HttpClient of Zuul by creating a custom bean of CloseableHttpClient. there you can set your proxy configuration.

@Bean
@Primary
public CloseableHttpClient customHttpClient() {
return HttpClientBuilder.create()
                        .setSSLHostnameVerifier(new NoopHostnameVerifier())
                        .setProxy(new HttpHost("your.proxy.com", 80))
                        .build();
}

If you want to use different proxies for your routes take a look at my answer in this post: https://stackoverflow.com/a/72207020/19096791

Please also take note that zuul is not longer supported by spring cloud. so the recommended way is to switch to spring gateway.

hope i could helped you

Upvotes: 1

the_dude
the_dude

Reputation: 26

There's no way to do it. Zuul is just not meant for that kind of things.

Upvotes: 1

Related Questions