SpringCloud-Zuul forwarded to modify the filter url


  After using springcloud, will be used to zuul natural gateway to forward the request to the respective corresponding micro-up service. Zuul use of filters can verify access to a number of interfaces, and sometimes nature needs to be some rules url Some corresponding forwarding operation, which would allow the front end you do not know the real Road King back end, you can adapt some of their own needs. url modify code when first affixed forwarding.

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

@Component
public class UrlRedirectFilter extends ZuulFilter implements AbstractLogger{

  /**
   * Redirect rules, according to the key to redirect to val.
   */
  private static Map<String, String>urlMap=new HashMap<>();
  static {
    urlMap.put("t", "/test/");
  }
  @Override
  public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    String url = request.getRequestURI(); // 列子 [/user/login/loginWx]
    String [] split = url.split ( "/", 3); // here to cut it, so to determine whether the need to modify the url below.
    if (split.length>=2) {
      String val = urlMap.get(split[1]);
      if (StringUtils.isNotEmpty(val)) {
        url = url.replaceFirst ( "/" + split [1] + "/", val); // url good to be replaced according to the configuration, there can write their own rules of conversion url
        ctx.put (FilterConstants.REQUEST_URI_KEY, url); // url to replace the SET in, will be used in the url corresponding transfer request url
      }
    }
    return null;
  }

  @Override
  public boolean shouldFilter() {
    return true;
  }

  // filterOrder: order filtering
  @Override
  public int filterOrder() {
    return 1;
  }

  / * (Non-Javadoc) filterType: Returns a string representative of the type of filter, the filter type defines four different zuul in the life cycle, as follows: 
  pre: Before routing
  routing: The routing
  post: After routing
  error: send error calling
  */
  @Override
  public String filterType() {
    return FilterConstants.ROUTE_TYPE;
  }
}

  

  I used here zuul filter to intercept modify the forwarding url, the code is set up in the routing of the filter, this place is particularly important, it must be routing of, or no effect. Then at the time of routing the first one to modify the forwarding url, and every time into the run method, because shouldFilter () always give is true.

First of all

  In org.springframework.cloud.netflix.zuul.filters.pre.PreDecorationFilter this decision which filter has a route of ctx.put (REQUEST_URI_KEY, route.getPath ()) calls as uri; this line of code, it was in this put a value corresponding to this url, url modify and then we will put into it, zuul will be forwarded at the time, that is SendForwardFilter the inside (posted a source)

@Override
public Object run() {
  try {
    RequestContext ctx = RequestContext.getCurrentContext();
    String path = (String) ctx.get(FORWARD_TO_KEY);
    RequestDispatcher dispatcher = ctx.getRequest().getRequestDispatcher(path);
    if (dispatcher != null) {
      ctx.set(SEND_FORWARD_FILTER_RAN, true);
      if (!ctx.getResponse().isCommitted()) {
        dispatcher.forward(ctx.getRequest(), ctx.getResponse());
        ctx.getResponse().flushBuffer();
      }
    }
  }catch (Exception ex) {
    ReflectionUtils.rethrowRuntimeException(ex);
  }
  return null;
}

Here take this key is FORWARD_TO_KEY stored url. That is ultimately forwarded url value.

So far, url forwarding modification is completed ..

 

Guess you like

Origin www.cnblogs.com/liboware/p/12573709.html