Redirecting calls using ZuulProxy

依然范特西╮ 提交于 2019-12-24 00:28:34

问题


What I want to do:

I would like to have a server that will redirect some kind of calls this way:

http://localhost:8080/myapp/api/rest/category/method

in

http://localhost:8090/category/api/method.

What I've done:

I've configured my Zuul Proxy like this:

info:
  component: Simple Zuul Proxy Server

cloud:
  conablefig:
    failFast: true
    discovery:
      end: true

zuul:
  routes:
    api: /myapp/api/rest/**

server:
  port: 8080

logging:
  level:
    ROOT: INFO
    org.springframework.web: INFO
    info.tcb: DEBUG

I'm using a route filter this way:

@Slf4j
public class SimpleRouteFilter extends ZuulFilter {

    @Value("${unauthorized.url.redirect:http://localhost:8090/testcategory/api/available}")
    private String urlRedirect;

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

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

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

    private boolean isAuthorizedRequest(HttpServletRequest request) {
        return false;
    }

    private void setRouteHost(RequestContext ctx) throws MalformedURLException {

        String urlS = ctx.getRequest().getRequestURL().toString();
        URL url = new URL(urlS);
        String protocol = url.getProtocol();
        String rootHost = url.getHost();
        int port = url.getPort();
        String portS = (port > 0 ? ":" + port : "");
        ctx.setRouteHost(new URL(protocol + "://" + rootHost + portS));
    }

    @Override
    public Object run() {
        log.debug("query interception");
        RequestContext ctx = RequestContext.getCurrentContext();
        try {
            if (isAuthorizedRequest(ctx.getRequest())) {
                log.debug("NOT redirecting...");
                setRouteHost(ctx);
            } else {
                log.debug("redirecting to " + urlRedirect + " ...");
                ctx.setRouteHost(new URL(urlRedirect));
            }
        } catch (MalformedURLException e) {
            log.error("", e);
        }
        return null;
    }
}

I'm using a default url in this class just for testing redirect.

My problem is that when I call for example http://localhost:8080/myapp/api/rest/testcategory/available it ends into a 404, but my filter is logging that it performs the redirect.

It only works if I'll call http://localhost:8080/myapp/api/rest/.

What am I doing wrong? Why the double ** isn't working for all other calls?

来源:https://stackoverflow.com/questions/38248648/redirecting-calls-using-zuulproxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!