spring boot filter 配置多个时,执行顺序

spring boot 配置Filter过滤器 中简单介绍了spring boot 中如何添加过滤器,有人问到若是配置多个怎么控制,先通过哪一个过滤器,后通过哪一个过滤器。在web.xml中,咱们知道,执行顺序是谁在前边执行谁。java

在spring boot中的FilterRegistrationBean注册过滤器的类中有个order属性,web

private int order = Ordered.LOWEST_PRECEDENCE;

细看源码能够知道,这个order的默认值是Integer.MAX_VALUE 也就是int的最大值,spring

spring boot 会按照order值的大小,从小到大的顺序来依次过滤。session

spring boot 配置Filter过滤器 中能够这样修改svg

/** * 配置过滤器 * @return */
    @Bean
    public FilterRegistrationBean someFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(sessionFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("paramName", "paramValue");
        registration.setName("sessionFilter");
        registration.setOrder(Integer.MAX_VALUE);
        return registration;
    }

再有一个过滤器的话,能够设置成 registration.setOrder(Integer.MAX_VALUE - 1)spa


spring boot也提供了注解的方式,例如.net

/** * 配置过滤器 * @return */
    @Bean
    @Order(Integer.MAX_VALUE)
    public FilterRegistrationBean someFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(sessionFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("paramName", "paramValue");
        registration.setName("sessionFilter");
        return registration;
    }

上面两种方法都行,想用那种看你喜欢。。。。code