码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot 之FilterRegistrationBean --支持web Filter 排序的使用(转)

时间:2019-12-04 17:18:38      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:sdn   设置   cto   throw   practice   org   文章   ann   framework   

Spring Boot 之FilterRegistrationBean  --支持web Filter 排序的使用
Spring 提供了FilterRegistrationBean类,此类提供setOrder方法,可以为filter设置排序值,
让spring在注册web filter之前排序后再依次注册。

  写一个普通的filter:

package com.sdcuike.practice.web2;
 
import java.io.IOException;
 
import javax.annotation.Resource;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import com.sdcuike.practice.config.CommonConfig;
 
public class FilterDemo3 implements Filter {
    private final Logger log = LoggerFactory.getLogger(getClass());
 
    @Resource
    private CommonConfig commonConfig;
 
    @Override
    public void destroy() {
        log.info("" + getClass() + " destroy");
 
    }
 
    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
        log.info("" + getClass() + " doFilter " + commonConfig);
        arg2.doFilter(arg0, arg1);
 
    }
 
    @Override
    public void init(FilterConfig arg0) throws ServletException {
        log.info("" + getClass() + " init");
 
    }
 
}

配置如下:

package com.sdcuike.practice.web2;
 
import javax.servlet.Filter;
 
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import com.sdcuike.spring.extend.web.MvcConfigurerAdapter;
 
/**
 * web 组件配置
 * 
 * @author sdcuike
 *         <p>
 *         Created on 2017-02-10
 *         <p>
 *         自定义注入,并支持依赖注入,组件排序
 */
@Configuration
public class WebComponent2Config   {
 
    @Bean
    public FilterRegistrationBean filterDemo3Registration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(filterDemo3());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("paramName", "paramValue");
        registration.setName("filterDemo3");
        registration.setOrder(6);
        return registration;
    }
 
    @Bean
    public FilterRegistrationBean filterDemo4Registration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(filterDemo4());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("paramName", "paramValue");
        registration.setName("filterDemo4");
        registration.setOrder(7);
        return registration;
    }
 
    @Bean
    public Filter filterDemo3() {
        return new FilterDemo3();
    }
 
    @Bean
    public Filter filterDemo4() {
        return new FilterDemo4();
    }
 
}
利用这种方式,我们可以对filter排序,可自行测试,源码:


————————————————
版权声明:本文为CSDN博主「A_Beaver」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/doctor_who2004/article/details/56055505

Spring Boot 之FilterRegistrationBean --支持web Filter 排序的使用(转)

标签:sdn   设置   cto   throw   practice   org   文章   ann   framework   

原文地址:https://www.cnblogs.com/muxi0407/p/11984085.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!