简体   繁体   English

Spring 引导和 Zuul 路由

[英]Spring Boot and Zuul routes

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但是代理发送的 web 页面具有如下链接

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

A must be of the form like: A 必须是如下形式:

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: 1.直接从Internet访问myserver时如:

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: 2.当从 Internet 访问代理时,例如:

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

proxy-server sends the page with the links like:代理服务器发送带有以下链接的页面:

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

Understood and tried different options.了解并尝试了不同的选择。
All I needed to solve the problem was add to the settings:解决问题所需的只是添加到设置中:
.properties 。特性

......
zuul.add-host-header = true 
......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM