我有重定向到x.jsp的下游服务,例如,此位置不在网关路由中
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头的网关。我该怎么做呢?
发布于 2017-03-30 04:11:21
你的下游应用程序应该尊重x-forwarded-*头文件,而不是像这样生成重定向。但下面的筛选器将为您更改重定向位置:
@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;
}https://stackoverflow.com/questions/42191353
复制相似问题