• SpringBoot添加过滤器


    方式一(不可排序)

    1. 新建过滤器实现javax.servlet.Filter.
    2. 应用添加@ServletComponentScan.
    3. 过滤器添加注解@WebFilter(name, urlPattern).

    方式二(可排序)

    1. 新建过滤器实现javax.servlet.Filter.
    2. 新建配置类并注入FilterRegistrationBean,使用该对象的setOrder(),addUrlPattern()等方法实现。                                                                                                                 

    应用启动

    package com.guofengzhi.helloworld;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    
    @SpringBootApplication
    //@ServletComponentScan方式一添加
    public class HelloworldApplication {
    
        public static void main(String[] args) {
    
            SpringApplication.run(HelloworldApplication.class, args);
        }
    
    }

    过滤器类

    package com.guofengzhi.helloworld.filter;
    
    import org.springframework.core.annotation.Order;
    
    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(filterName = "a", urlPatterns = "/*")方式一需要添加,方式二不需要
    public class AFilter implements Filter { 

      @Override
      
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println(
    "init a filter...");
      }

      @Override
      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println(
    "do a filter...");
        filterChain.doFilter(servletRequest, servletResponse);
      }

      @Override
      public void destroy() {
        System.out.println(
    "destory a filter...");
      }
    }

    FilterRegistrationBean配置类

    package com.guofengzhi.helloworld.filter;
    
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class FilterRegistrationBeanConf {
    
        @Bean
        public FilterRegistrationBean<AFilter> aFilter() {
            FilterRegistrationBean<AFilter> filterRegistrationBean = new FilterRegistrationBean<>();
            filterRegistrationBean.setFilter(new AFilter());
            filterRegistrationBean.setOrder(1);
            filterRegistrationBean.addUrlPatterns("/*");
            return filterRegistrationBean;
        }
    
        @Bean
        public FilterRegistrationBean<BaseFilter> baseFilter() {
            FilterRegistrationBean<BaseFilter> filterRegistrationBean = new FilterRegistrationBean<>();
            filterRegistrationBean.setFilter(new BaseFilter());
            filterRegistrationBean.setOrder(2);
            filterRegistrationBean.addUrlPatterns("/*");
            return filterRegistrationBean;
        }
    
        @Bean
        public FilterRegistrationBean<WorkFilter> workFilter() {
            FilterRegistrationBean<WorkFilter> filterRegistrationBean = new FilterRegistrationBean<>();
            filterRegistrationBean.setFilter(new WorkFilter());
            filterRegistrationBean.setOrder(3);
            filterRegistrationBean.addUrlPatterns("/*");
            return filterRegistrationBean;
        }
    
        @Bean
        public FilterRegistrationBean<CompleteFilter> completeFilter() {
            FilterRegistrationBean<CompleteFilter> filterRegistrationBean = new FilterRegistrationBean<>();
            filterRegistrationBean.setFilter(new CompleteFilter());
            filterRegistrationBean.setOrder(4);
            filterRegistrationBean.addUrlPatterns("/*");
            return filterRegistrationBean;
        }
    }

     

  • 相关阅读:
    javaWeb快速入门+——初体验-HelloWorld
    简单理解Linux系统的挂载是什么鬼
    STM32MP157 Cortex®-M4高性能系列MCU
    常见六种锂电池特性及参数对比
    IIC通信详解
    stm32微秒延时问题
    STM32 HAL库实现微秒级别延时
    开关电源波纹的产生、测量及抑制
    图解DIY 1pA超微电流测试器
    stm32
  • 原文地址:https://www.cnblogs.com/guofz/p/15011296.html
Copyright © 2020-2023  润新知