• 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;
        }
    }

     

  • 相关阅读:
    Custom Roles Based Access Control (RBAC) in ASP.NET MVC Applications
    How To Display Variable Value In View?
    How do negative margins in CSS work and why is (margin-top:-5 != margin-bottom:5)?
    Async/Await FAQ (Stephen Toub)
    Async and Await (Stephen Cleary)
    Change Assembly Version in a compiled .NET assembly
    C# Under the Hood: async/await (Marko Papic)
    Bootstrap form-group and form-control
    Infralution.Localization.Wpf
    cocos2d-x 图形绘制
  • 原文地址:https://www.cnblogs.com/guofz/p/15011296.html
Copyright © 2020-2023  润新知