0
春天開機+ entflix zuul,當我在zuul從路由過濾器重定向到不同的端點比原來的崗位PARAM裏面是空的會被丟棄。當請求從zuul着diferent端點,一些之後的參數獲取DROP掉
任何想法?
春天開機+ entflix zuul,當我在zuul從路由過濾器重定向到不同的端點比原來的崗位PARAM裏面是空的會被丟棄。當請求從zuul着diferent端點,一些之後的參數獲取DROP掉
任何想法?
其怪,還是coulndt找出確切原因是什麼。正如通常HttpServelteRequest POST請求與原來一樣。但是在zuul中,請求對象被zuul neflix請求包裝,或許會發生。可能是netflix傢伙可以回答這個問題。
最近我能找到這個讓Netflix的上下文對象請求和響應門面,然後重定向一個解決方法。它爲我工作。
import java.lang.reflect.Field;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponseWrapper;
import org.apache.catalina.connector.RequestFacade;
import org.apache.catalina.connector.ResponseFacade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.http.HttpServletRequestWrapper;
/**
*
* @author Sivakumaran Kathamutthu
*
*/
@Component
public class RouteFilter extends ZuulFilter {
private static final Logger LOGGER = LoggerFactory.getLogger(RouteFilter.class);
@Override
public String filterType() {
return "route";
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return redirectFrom != null && !redirectFrom.trim().isEmpty()
&& RequestContext.getCurrentContext().getRequest().getRequestURI().contains(redirectFrom);
}
@Override
public Object run() {
RequestContext context = RequestContext.getCurrentContext();
setRouteHost(context);
return null;
}
/**
* This method allows to set the route host into the Zuul request context
* provided as parameter. The url is extracted from the orginal request and
* the host is extracted from it.
*
* @param context the provided Zuul request context
*/
private void setRouteHost(RequestContext context) {
String requestURI = context.getRequest().getRequestURI();
try {
Field requestField = ReflectionUtils.findField(HttpServletRequestWrapper.class, "req", HttpServletRequest.class);
if (requestField != null) {
requestField.setAccessible(true);
// Get the com.netflix.zuul.http.HttpServletRequestWrapper
HttpServletRequestWrapper zuulWrappedRequest = (HttpServletRequestWrapper) ReflectionUtils.getField(requestField, ((HttpServletRequestWrapper) context.getRequest()).getRequest());
// Get the requestFacade where original request resides
RequestFacade requestFacade = (RequestFacade) ReflectionUtils.getField(requestField, zuulWrappedRequest);
// Get the responseFacade where original response resides
ResponseFacade responseFacade = (ResponseFacade) ((HttpServletResponseWrapper) context.getResponse()).getResponse();
requestFacade.getRequestDispatcher("home/home.jsp").forward(requestFacade, responseFacade);
} else {
throw new RuntimeException("req field is not availble in HTTPServletRequest");
}
} catch (Exception ex) {
throw e;
}
}
}
工作正常,我。感謝您的解決方案 –