spring cloud zuul过滤器修改requestURI 忽略大小写
通过zuul网关处理requestURI可以做很多事情,如对uri的解密,转发,大小写转化等。
这里对URI做一个简单的大小写的转化。
写一个filter实现ZuulFilter:
package com.cis.mr.audit.config; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE; /** * @author :hkk * @date :Created in 2019/7/11 9:45 */ public class CaseInsenistiveFilter extends ZuulFilter { @Override public String filterType() { return PRE_TYPE; } @Override public int filterOrder() { return 4; } @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); String requestURI = request.getRequestURI(); HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(request){ @Override public String getRequestURI() { return requestURI.toLowerCase(); } }; ctx.setRequest(requestWrapper); return null; } }
注意这里有几点:
1. filterType是PRE_TYPE ,这是zuul定义的常量。可以通过FilterConstants查看。PRE_TYPE如下图,表示在处理转发请求之前的操作。
2. flterOrder是4,为什么是4,这里要了解zuul几个自带的filter。特别是PreDecorationFilter
下面是几个常用filter的order:
/** * Filter Order for {@link DebugFilter#filterOrder()}. */ public static final int DEBUG_FILTER_ORDER = 1; /** * Filter Order for * {@link org.springframework.cloud.netflix.zuul.filters.pre.FormBodyWrapperFilter#filterOrder()}. */ public static final int FORM_BODY_WRAPPER_FILTER_ORDER = -1; /** * Filter Order for * {@link org.springframework.cloud.netflix.zuul.filters.pre.PreDecorationFilter}. */ public static final int PRE_DECORATION_FILTER_ORDER = 5; /** * Filter Order for * {@link org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter#filterOrder()}. */ public static final int RIBBON_ROUTING_FILTER_ORDER = 10; /** * Filter Order for * {@link org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter#filterOrder()}. */ public static final int SEND_ERROR_FILTER_ORDER = 0; /** * Filter Order for {@link SendForwardFilter#filterOrder()}. */ public static final int SEND_FORWARD_FILTER_ORDER = 500; /** * Filter Order for * {@link org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter#filterOrder()}. */ public static final int SEND_RESPONSE_FILTER_ORDER = 1000; /** * Filter Order for * {@link org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter#filterOrder()}. */ public static final int SIMPLE_HOST_ROUTING_FILTER_ORDER = 100; /** * filter order for {@link Servlet30WrapperFilter#filterOrder()}. */ public static final int SERVLET_30_WRAPPER_FILTER_ORDER = -2; /** * filter order for * {@link org.springframework.cloud.netflix.zuul.filters.pre.ServletDetectionFilter#filterOrder()}. */ public static final int SERVLET_DETECTION_FILTER_ORDER = -3;
可以看到PreDecorationFilter的order是5,在它之前的filter,我们设置一个getRequestURI的值,就可以实现需求,PreDecorationFilter是zuul很重要的过滤器,推荐大家仔细了解一下。
最后,我们注入这上fitler。
@Bean public CaseInsenistiveFilter getCaseFilter() { return new CaseInsenistiveFilter(); }