美文网首页Java
Spring Boot 使用Filter

Spring Boot 使用Filter

作者: 西安法律咨询服务平台与程序员 | 来源:发表于2020-05-07 19:52 被阅读0次

    Spring Boot使用Filter有两种方式:

    方式一,使用FilterRegistrationBean配置

    @RestController
    @Slf4j
    public class FilterDemoController {
        @GetMapping("hello")
        public String hello(){
            log.info("FilterDemoController hello");
            return "hello";
        }
    }
    
    package com.mervyn.filterdemo.filter;
    
    import lombok.extern.slf4j.Slf4j;
    
    import javax.servlet.*;
    import java.io.IOException;
    @Slf4j
    public class FilterOne implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            log.info("FilterOne init");
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            log.info("FilterOne doFilter");
            filterChain.doFilter(servletRequest,servletResponse);
        }
    
        @Override
        public void destroy() {
            log.info("FilterOne destroy");
        }
    }
    
    package com.mervyn.filterdemo.filter;
    
    import lombok.extern.slf4j.Slf4j;
    
    import javax.servlet.*;
    import java.io.IOException;
    
    @Slf4j
    public class FilterTwo implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            log.info("FilterTwo init");
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            log.info("FilterTwo doFilter");
            filterChain.doFilter(servletRequest,servletResponse);
        }
    
        @Override
        public void destroy() {
            log.info("FilterTwo destroy");
        }
    }
    
    package com.mervyn.filterdemo.config;
    
    import com.mervyn.filterdemo.filter.FilterOne;
    import com.mervyn.filterdemo.filter.FilterTwo;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class FilterConfig {
        @Bean
        public FilterRegistrationBean registFilterOne() {
            FilterRegistrationBean registration = new FilterRegistrationBean();
            registration.setFilter(new FilterOne());
            registration.addUrlPatterns("/hello");
            registration.setName("FilterOne");
            registration.setOrder(1);
            return registration;
        }
    
        @Bean
        public FilterRegistrationBean registFFilterTwo() {
            FilterRegistrationBean registration = new FilterRegistrationBean();
            registration.setFilter(new FilterTwo());
            registration.addUrlPatterns("/hello");
            registration.setName("FilterTwo");
            registration.setOrder(2);
            return registration;
        }
    }
    
    server:
      port: 80
    

    http://localhost/hello访问效果:

    2020-04-23 12:27:07.699  INFO 15690 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 714 ms
    2020-04-23 12:27:07.736  INFO 15690 --- [           main] com.mervyn.filterdemo.filter.FilterOne   : FilterOne init
    2020-04-23 12:27:07.736  INFO 15690 --- [           main] com.mervyn.filterdemo.filter.FilterTwo   : FilterTwo init
    2020-04-23 12:27:07.826  INFO 15690 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
    2020-04-23 12:27:07.949  INFO 15690 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 80 (http) with context path ''
    2020-04-23 12:27:07.952  INFO 15690 --- [           main] c.m.filterdemo.FilterdemoApplication     : Started FilterdemoApplication in 16.321 seconds (JVM running for 16.622)
    2020-04-23 12:27:27.581  INFO 15690 --- [p-nio-80-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2020-04-23 12:27:27.582  INFO 15690 --- [p-nio-80-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
    2020-04-23 12:27:27.585  INFO 15690 --- [p-nio-80-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 3 ms
    2020-04-23 12:27:27.591  INFO 15690 --- [p-nio-80-exec-1] com.mervyn.filterdemo.filter.FilterOne   : FilterOne doFilter
    2020-04-23 12:27:27.591  INFO 15690 --- [p-nio-80-exec-1] com.mervyn.filterdemo.filter.FilterTwo   : FilterTwo doFilter
    2020-04-23 12:27:27.602  INFO 15690 --- [p-nio-80-exec-1] c.m.f.controller.FilterDemoController    : FilterDemoController hello
    2020-04-23 12:27:35.493  INFO 15690 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
    2020-04-23 12:27:35.505  INFO 15690 --- [extShutdownHook] com.mervyn.filterdemo.filter.FilterOne   : FilterOne destroy
    2020-04-23 12:27:35.506  INFO 15690 --- [extShutdownHook] com.mervyn.filterdemo.filter.FilterTwo   : FilterTwo destroy
    

    方式二,使用@WebFilter注解

    package com.mervyn.filterdemo.filter;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Component;
    
    import javax.servlet.*;
    import javax.servlet.annotation.WebFilter;
    import java.io.IOException;
    @Slf4j
    @Component
    @WebFilter(urlPatterns = "/hello", filterName = "filterOne")
    public class FilterOne implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            log.info("FilterOne init");
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            log.info("FilterOne doFilter");
            filterChain.doFilter(servletRequest,servletResponse);
        }
    
        @Override
        public void destroy() {
            log.info("FilterOne destroy");
        }
    }
    
    package com.mervyn.filterdemo.filter;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    import javax.servlet.*;
    import javax.servlet.annotation.WebFilter;
    import java.io.IOException;
    
    @Slf4j
    @Component
    @WebFilter(urlPatterns = "/hello", filterName = "filterTwo")
    public class FilterTwo implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            log.info("FilterTwo init");
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            log.info("FilterTwo doFilter");
            filterChain.doFilter(servletRequest,servletResponse);
        }
    
        @Override
        public void destroy() {
            log.info("FilterTwo destroy");
        }
    }
    
    2020-04-23 13:32:20.574  INFO 15767 --- [           main] com.mervyn.filterdemo.filter.FilterTwo   : FilterTwo init
    2020-04-23 13:32:20.574  INFO 15767 --- [           main] com.mervyn.filterdemo.filter.FilterOne   : FilterOne init
    2020-04-23 13:32:20.658  INFO 15767 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
    2020-04-23 13:32:20.777  INFO 15767 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 80 (http) with context path ''
    2020-04-23 13:32:20.780  INFO 15767 --- [           main] c.m.filterdemo.FilterdemoApplication     : Started FilterdemoApplication in 16.217 seconds (JVM running for 16.506)
    2020-04-23 13:32:50.464  INFO 15767 --- [p-nio-80-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2020-04-23 13:32:50.465  INFO 15767 --- [p-nio-80-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
    2020-04-23 13:32:50.469  INFO 15767 --- [p-nio-80-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 4 ms
    2020-04-23 13:32:50.475  INFO 15767 --- [p-nio-80-exec-1] com.mervyn.filterdemo.filter.FilterOne   : FilterOne doFilter
    2020-04-23 13:32:50.475  INFO 15767 --- [p-nio-80-exec-1] com.mervyn.filterdemo.filter.FilterTwo   : FilterTwo doFilter
    2020-04-23 13:32:50.487  INFO 15767 --- [p-nio-80-exec-1] c.m.f.controller.FilterDemoController    : FilterDemoController hello
    2020-04-23 13:32:53.476  INFO 15767 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
    2020-04-23 13:32:53.490  INFO 15767 --- [extShutdownHook] com.mervyn.filterdemo.filter.FilterTwo   : FilterTwo destroy
    2020-04-23 13:32:53.490  INFO 15767 --- [extShutdownHook] com.mervyn.filterdemo.filter.FilterOne   : FilterOne destroy
    

    这种方式无法指定Filter的顺序。

    相关文章

      网友评论

        本文标题:Spring Boot 使用Filter

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