首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编写netflix zuul过滤器来更改响应位置标头属性?

问如何编写netflix zuul过滤器来更改响应位置标头属性?
EN

Stack Overflow用户
提问于 2017-02-13 02:14:56
回答 1查看 3.2K关注 0票数 2

我有重定向到x.jsp的下游服务,例如,此位置不在网关路由中

代码语言:javascript
复制
gateway route --- localhost:8080/app - 192.168.1.1:80 (DownStream app)

在DownStream应用程序中,当用户未登录时,它会重定向到192.168.1.1:80/Login.jsp,该地址位于响应的Location头中。

此URL未使用网关。

我想写一个zuul过滤器来改变这个重定向的网址,通过映射在zuul路由动态,例如,为每个网址,在zuul过滤器更改Location头的网关。我该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-30 04:11:21

你的下游应用程序应该尊重x-forwarded-*头文件,而不是像这样生成重定向。但下面的筛选器将为您更改重定向位置:

代码语言:javascript
复制
@Override
public String filterType() {
    return "post";
}

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

@Override
public boolean shouldFilter() {
    int status = RequestContext.getCurrentContext().getResponseStatusCode();
    return status >= 300 && status < 400;
}

@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    Map<String, String> requestHeaders = ctx.getZuulRequestHeaders();
    Optional<Pair<String, String>> locationHeader = ctx.getZuulResponseHeaders()
                .stream()
                .filter(stringStringPair -> LOCATION.equals(stringStringPair.first()))
                .findFirst();

        if (locationHeader.isPresent()) {
            String oldLocation = locationHeader.get().second();
            String newLocation = ...
            locationHeader.get().setSecond(newLocation);
        }
    return null;
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42191353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档