0

我有下游服務重定向至x.jsp這個位置不是在網關路由例如如何編寫netflix zuul過濾器來更改響應位置標題屬性?

gateway route --- localhost:8080/app - 192.168.1.1:80 (DownStream app) 

下游應用時在沒有登錄用戶,它重定向到192.168.1.1:80/Login。 jsp,它位於響應的Location標題中。

此URL不使用網關。

我想編寫一個zuul過濾器,通過在zuul路由中進行映射來更改此重定向url,例如對於每個由zuul過濾器更改Location標頭的網址進行路由。我怎樣才能做到這一點?

+0

那麼你是否嘗試使用Zuul過濾器來做到這一點,它不起作用? –

+0

是不行的 –

+0

你能否提供樣本申請? –

回答

2

您的下游應用程序應該尊重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; 
} 
相關問題