当请求转发到zuul的不同端点时,某些post参数会被droped

时间:2017-06-29 11:15:01

标签: spring spring-boot netflix-zuul netflix

在spring boot + entflix zuul中,当我从zuul中的路由过滤器重定向到与原始路径不同的端点时,空的post param会被丢弃。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

它的奇怪,仍然可以找出确切的原因。通常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;    
    }

  }

}