美文网首页
@WebFilter注解过滤器

@WebFilter注解过滤器

作者: 冷月成双 | 来源:发表于2020-03-04 15:44 被阅读0次

    @WebFilter注解过滤器

    @WebFilter加在过滤器的注解上使用

    import lombok.extern.slf4j.Slf4j;
    
    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 javax.servlet.annotation.WebFilter;
    import java.io.IOException;
    
    @WebFilter(urlPatterns = "/*")
    @Slf4j
    public class WebFilterTest implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            log.info("WebFilterTest --- init");
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            log.info("WebFilterTest --- doFilter");
            filterChain.doFilter(servletRequest,servletResponse);
        }
    
        @Override
        public void destroy() {
            log.info("WebFilterTest --- destroy");
        }
    }
    

    @WebFilter源码:

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface WebFilter {
        String description() default "";
        /**Filter显示名*/
        String displayName() default "";
        /**配置参数*/
        WebInitParam[] initParams() default {};
        /**Filter名称*/
        String filterName() default "";
    
        String smallIcon() default "";
    
        String largeIcon() default "";
        /**指定对哪些Servlet进行过滤*/
        String[] servletNames() default {};
        /**指定拦截的路径*/
        String[] value() default {};
        /**指定拦截的路径*/
        String[] urlPatterns() default {};
        /**指定Filter对哪种方式的请求进行过滤*/
        DispatcherType[] dispatcherTypes() default {DispatcherType.REQUEST};
        /**指定Filter是否支持异步模式*/
        boolean asyncSupported() default false;
    }
    

    在springBoot的启动类中加入注解:

    import org.springframework.boot.web.servlet.ServletComponentScan;
    
    @ServletComponentScan
    

    多个@WebFilter注解的过滤器可以配合@Order()注解实现执行过滤的顺序

    import org.springframework.core.annotation.Order;
    
    @WebFilter(urlPatterns = "/*")
    @Slf4j
    @Order(1)
    public class WebFilterTest implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            log.info("WebFilterTest --- init");
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            log.info("WebFilterTest --- doFilter");
            filterChain.doFilter(servletRequest,servletResponse);
        }
    
        @Override
        public void destroy() {
            log.info("WebFilterTest --- destroy");
        }
    }
    
    @WebFilter(urlPatterns = "/*")
    @Slf4j
    @Order(2)
    public class WebFilterTest2 implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            log.info("2---WebFilterTest2 --- init");
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            log.info("2 --- WebFilterTest2 --- doFilter");
            filterChain.doFilter(servletRequest,servletResponse);
        }
    
        @Override
        public void destroy() {
            log.info("WebFilterTest2 --- destroy");
        }
    }
    

    执行结果

    WebFilterTest --- doFilter
    2 --- WebFilterTest2 --- doFilter
    

    不使用注解的方式使用过滤器

    创建过滤器类

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

    注册过滤器

    @Configuration
    public class FilterConfig {
    
        @Bean
        public WebFilterTest webFilterTest(){
            return new WebFilterTest();
        }
    
        @Bean
        public FilterRegistrationBean filterRegist(){
            FilterRegistrationBean frBean = new FilterRegistrationBean();
            frBean.setFilter(webFilterTest());
            frBean.setOrder(1);
            frBean.addUrlPatterns("/*");
            return  frBean;
        }
    }
    

    多个过滤器注册

    再添加一个过滤器:

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

    修改配置类:

    @Configuration
    public class FilterConfig {
    
        @Bean
        public WebFilterTest webFilterTest(){
            return new WebFilterTest();
        }
         @Bean
        public WebFilterTest2 webFilterTest2(){
            return new WebFilterTest2();
        }
    
        @Bean
        public FilterRegistrationBean filterRegist(){
            FilterRegistrationBean frBean = new FilterRegistrationBean();
            frBean.setFilter(webFilterTest());
            frBean.setOrder(1);
            frBean.addUrlPatterns("/*");
            return  frBean;
        }
        @Bean
        public FilterRegistrationBean filterRegist2(){
            FilterRegistrationBean frBean = new FilterRegistrationBean();
            frBean.setFilter(webFilterTest2());
            frBean.setOrder(2);
            frBean.addUrlPatterns("/*");
            return  frBean;
        }
    }
    

    本篇文章由一文多发平台ArtiPub自动发布

    相关文章

      网友评论

          本文标题:@WebFilter注解过滤器

          本文链接:https://www.haomeiwen.com/subject/njtblhtx.html