zoukankan      html  css  js  c++  java
  • Springboot中Filter的使用

    在Springboot中使用Filter有两种方式,注解@WebFilter方式和注册bean方式;

    一、注解@WebFilter方式

    1. 实现Filter接口(javax.servlet);

    2. 添加@WebFilter注解;

    3. 启动类添加@ServletComponmentScan注解;

    附上代码:

    第一个Filter

    @Slf4j
    @WebFilter(filterName = "filter1", urlPatterns = {"/url1/*"})
    public class Filter1_Filter implements Filter {
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            log.info("do filter1");
            filterChain.doFilter(servletRequest, servletResponse);
        }
    
        @Override
        public void destroy() {
            
        }
    
    }

    第二个Filter

    @Slf4j
    @WebFilter(filterName = "filter2", urlPatterns = {"/url2/*"})
    public class Filter2_Filter implements Filter {
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            log.info("do filter2");
            filterChain.doFilter(servletRequest, servletResponse);
        }
    
        @Override
        public void destroy() {
            
        }
    
    }

    启动类:

    @SpringBootApplication
    @ServletComponentScan
    public class MyApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MyApplication .class, args);
        }
    
    }

    下面说说我踩过的坑:

    1. 不加@ServletComponentScan注解无法注入Filter,这个注解的作用就是去扫描@WebServlet、@WebFilter、@WebListener这类特殊注解。如果不加,spring容器是不会主动扫描的

    2. 多个Filter的执行顺序,说出来你可能不信,这种注解方式实现的Filter是通过Filter的类名首字母来控制执行顺序的,像Filter1_Filter就比Filter2_Filter先执行。但网上一些资料说,当Filter数量很多时,是没有明确的执行顺序的,所以这种方式还是放弃吧,推荐第二种。

    二、注册bean的方式

    1、实现Filter接口

    2、注册Filter

    下面附上代码,第一个Filter:

    @Slf4j
    public class Filter1 implements Filter {
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            log.info("do filter1");
            filterChain.doFilter(servletRequest, servletResponse);
        }
    
        @Override
        public void destroy() {
            
        }
    
    }

    第二个Filter:

    @Slf4j
    public class Filter2 implements Filter {
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            log.info("do filter2");
            filterChain.doFilter(servletRequest, servletResponse);
        }
    
        @Override
        public void destroy() {
            
        }
    
    }

    注册类:

    @Configuration
    public class FilterConfig {
    
        @Bean
        public Filter filter1(){
            return new Filter1();
        }
    
        @Bean
        public Filter filter2(){
            return new Filter2();
        }
    
        @Bean
        public FilterRegistrationBean setFilter1() {
            FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
            filterRegistrationBean.setFilter(filter1());
            filterRegistrationBean.addUrlPatterns("/url1/*");
            filterRegistrationBean.setOrder(1);   //order的数值越小,在所有的filter中优先级越高
            return filterRegistrationBean;
        }
    
        @Bean
        public FilterRegistrationBean setFilter2(){
            FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
            filterRegistrationBean.setFilter(filter2());
            filterRegistrationBean.addUrlPatterns("/url2/*");
            filterRegistrationBean.setOrder(2);   //order的数值越小,在所有的filter中优先级越高
            return filterRegistrationBean;
        }
    
    }

    这种方式就比较严谨了,通过filterRegistrationBean.setOrder(1)设置执行顺序亲测有效。

    注意:

    SpringBootApplication上使用@ServletComponentScan注解后,Servlet、Filter、Listener

    可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码。

    转载自:https://www.cnblogs.com/javafucker/p/10144958.html

  • 相关阅读:
    NO.05--谈一谈Angular 和 Vue.js 的对比。
    NO.04--我的使用心得之使用vue绑定class名
    No.03---Vue学习之路之模块化组织
    NO.02---聊聊Vue提升
    NO.01---今天聊聊Vuex的简单入门
    hello word!------为什么开通博客以及自我介绍
    JavaScript 常用正则示例
    Newtonsoft.Json(Json.Net)学习笔记
    aspx后台传递Json到前台的两种接收方法
    MVC5中后台提供Json,前台处理Json,绑定给Dropdownlist的例子
  • 原文地址:https://www.cnblogs.com/damoblog/p/13614517.html
Copyright © 2011-2022 走看看