Spring Boot过滤器Filter

Laughing
2019-07-26 / 0 评论 / 1,257 阅读 / 搜一下 / 正在检测是否收录...

单个过滤器

如果只是定义一个过滤器,直接通过@Configuration注解即可。

package Cc.LiSen.Configurations;  
  
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  
import org.springframework.context.annotation.Configuration;    
import javax.servlet.*;  
import javax.servlet.annotation.WebFilter;  
import java.io.IOException;  
  
/** 
 * ClassName: CustomFilter <br/> 
 * Description: <br/> 
 * date: 2019/7/25 22:04<br/> 
 * 
 * @since JDK 1.8 
 */  
@WebFilter(filterName = "CustomFilter", urlPatterns = "{/*}")  
public class CustomFilter implements Filter {  
    private Logger logger = LoggerFactory.getLogger(this.getClass());  
  
    @Override  
    public void init(FilterConfig filterConfig) throws ServletException {  
        logger.info("初始化过滤器CustomFilter");  
    }  
  
    
    @Override  
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {  
        logger.info("过滤器CustomFilter开始工作,并转入下一个过滤");  
        chain.doFilter(request, response);  
        logger.info("CustomFilter继续过滤");  
    }  
  
    @Override  
    public void destroy() {  
        logger.info("过滤器CustomFilter销毁");  
    }  
}

多个过滤器

如果定义多个过滤器,需要通过FilterRegistrationBean提供setOrder方法,可以为filter设置排序值,让spring在注册web filter之前排序后再依次注册。启动类中利用@bean注册FilterRegistrationBean

*温馨提示
过滤器定义与上面类似,去掉@Configuration注解即可,这里不再赘述,然后修改启动类,增加以下代码

package Cc.LiSen;  
  
import Cc.LiSen.Configurations.CustomFilter;  
import Cc.LiSen.Configurations.CustomFilterOther;  
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.boot.autoconfigure.domain.EntityScan;  
import org.springframework.boot.web.servlet.FilterRegistrationBean;  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.ComponentScan;  
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;  
  
  
@SpringBootApplication  
//SpringBoot 默认从App类往下面的包扫描  
//所以如果控制器、实体等类与App不在一个包,同时不在下面的包时,必须手动指定包  
@EnableJpaRepositories(basePackages = {"Cc.LiSen.Repositories", "Cc.LiSen.Services"})  
public class App {  
  
    private static final Logger logger = LoggerFactory.getLogger(App.class);  
  
    public static void main(String[] args) {  
//        System.setProperty("log.root","DEBUG,info,error");  
//  
//        System.setProperty("log.base","D:\\log4j\\base");  
        SpringApplication.run(App.class, args);  
    }  
  
    @Bean  
    public FilterRegistrationBean filterRegistrationBean() {  
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();  
        filterRegistrationBean.setFilter(new CustomFilter());  
        filterRegistrationBean.setOrder(10);  
        return filterRegistrationBean;  
    }  
  
    @Bean  
    public FilterRegistrationBean filterRegistrationBeanOther() {  
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();  
        filterRegistrationBean.setFilter(new CustomFilterOther());  
        filterRegistrationBean.setOrder(20);  
        return filterRegistrationBean;  
    }  
}
0

评论 (0)

取消
  1. 头像
    dire
    MacOS · Safari

    这个看起来不错,谢谢。

    回复