• ZuulFilter 执行顺序


    说明: 创建了两个Filter,分别是

    PreFilter

    public class PreFilter extends ZuulFilter {
        public PreFilter() {
            super();
        }
     
        @Override
        public String filterType() {
            return FilterConstants.PRE_TYPE;
        }
     
        @Override
        public int filterOrder() {
            return 0;
        }
     
        @Override
        public boolean shouldFilter() {
    //        return true;
     
    //跨域设置
            RequestContext ctx = RequestContext.getCurrentContext();
            HttpServletRequest request = ctx.getRequest();
            //只过滤OPTIONS 请求
            if(request.getMethod().equals(RequestMethod.OPTIONS.name())){
     
                return true;
            }
            return false;
        }
     
        @Override
        public Object run() {
     
            RequestContext ctx = RequestContext.getCurrentContext();
            HttpServletRequest request = ctx.getRequest();
            HttpServletResponse response = ctx.getResponse();
     
            response.setHeader("Access-Control-Allow-Origin",request.getHeader("Origin"));
            response.setHeader("Access-Control-Allow-Credentials","true");
            response.setHeader("Access-Control-Allow-Headers","authorization, content-type");
            response.setHeader("Access-Control-Allow-Methods","POST,GET");
     
            String requestUrl = request.getRequestURL().toString();
            String requestUri = request.getRequestURI();
            String zuul = requestUrl.substring(0,requestUrl.indexOf(requestUri)); // zuul根路径
            ctx.addZuulRequestHeader("zuul", zuul);
            //不再路由
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(200);
     
            return null;
        }
        
        
        
    }

    PostFilter

    @Component
    public class PostFilter extends ZuulFilter {
        protected static final String SEND_ERROR_FILTER_RAN = "sendErrorFilter.ran";
     
        @Override
        public String filterType() {
            return FilterConstants.POST_TYPE;
        }
     
        @Override
        public int filterOrder() {
            return -1;
        }
     
        @Override
        public boolean shouldFilter() {
            return true;
        }
     
        @Override
        public Object run() {
     
            return null;
        }
    }

    我测试了几种情况,发现结果如下


    1  按照filterType决定顺序

    Pre 优先 Post执行,此时filterOrder没有作用。

    2 filterType相同

    filterOrder有作用,数字越小,越先执行。(负数也是这个规则,0和-1的话,-1先执行)

    3 相同filterType,相同filterOrder,都执行,执行顺序不清楚。

    prefilter先执行了,post后执行了。

    感觉不像是按照过滤请名称排序的样子。

  • 相关阅读:
    PHP+MySQL实现海量数据导入导出的总结:is_numbric函数的坑
    【PHP开发规范】继承与扩展:PSR-2 编码风格规范
    【PHP开发规范】老生常谈的编码开发规范你懂多少?
    【PHP面试题】通俗易懂的两个面试必问的排序算法讲解:冒泡排序和快速排序
    php数组函数array_column:不用循环就能提取多维数组内容
    php使用urlencode对中文编码而引出的问题:urlencode和rawurlencode的区别
    table-tree vs stock vs whiteboard
    PDF解析
    山灵up4
    Devops之CI/CD
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/10782987.html
Copyright © 2020-2023  润新知