Spring Boot and Zuul routes
Asked Answered
K

1

7

There is a simple proxy:

@EnableZuulProxy
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    @Bean
    public  SimpleFilter simpleFilter(){
        return  new SimpleFilter();
    }

}

Pre filter:

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 properties:

    zuul.ignored-patterns=/myserver/web/**
    zuul.routes.myserver.path=/myserver/api/**
    zuul.routes.myserver.url=http://localhost:80/myserver/api
    zuul.routes.myserver.sensitiveHeaders = Cookie,Set-Cookie
    server.port=3000

In general, everything works well.
But the web pages that the proxy sends have links like

href="http://localhost:80/myserver/api/item"

A must be of the form like:

href="http://server_ip:3000/myserver/api/item"

How to configure a server to send the correct links?

Cases:
1.When accessing the myserver directly from the Internet like:

http://server_ip:80/myserver/api/item

server sends the page with the links like:

 href="http://server_ip:80/myserver/api/item"

2.When accessing the proxy from the Internet like:

http://server_ip:3000/myserver/api/item

proxy-server sends the page with the links like:

href="http://localhost:80/myserver/api/item"
Kidd answered 4/7, 2020 at 17:0 Comment(0)
K
3

Understood and tried different options.
All I needed to solve the problem was add to the settings:
.properties

......
zuul.add-host-header = true 
......
Kidd answered 14/7, 2020 at 6:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.