Springboot整合Filter

2021-02-18 09:19

阅读:644

标签:res   request   修改   组件   创建   let   url   扫描   code   

1,方式一

  【1】通过注解扫描完成Filter组件注册

 

/*@WebFilter(filterName = "FirstFilter",urlPatterns = {"*.do","*.jsp"})*/
@WebFilter(filterName = "FirstFilter",urlPatterns = "/first")
public class FirstFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入FirstFilter...");
     filterChain.doFilter(servletRequest,servletResponse); System.out.println(
"退出FirstFilter..."); } @Override public void destroy() { } }

 

 

 

  【2】修改启动类

 

@SpringBootApplication
@ServletComponentScan//在Spring Boot启动时会扫描@WebFiter注解,并将该类实例
public class SpringbootwebApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootwebApplication.class, args);
    }

}

 

2,方式二

  【1】通过方法完成Filter组件注册

 

public class SecondFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入SecondFilter...");
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("退出SecondFilter...");
    }

    @Override
    public void destroy() {

    }
}

 

 

 

  【2】创建Filter配置类

 

@Configuration
public class FilterConfig {
    @Bean
    public FilterRegistrationBean getFilterRegistrationBean()
    {
        FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
        /*bean.addUrlPatterns(new String[]{"*.do","*.jsp"});*/
        bean.addUrlPatterns("/second");
        return bean;
    };
}

 

Springboot整合Filter

标签:res   request   修改   组件   创建   let   url   扫描   code   

原文地址:https://www.cnblogs.com/yz-bky/p/12690821.html


评论


亲,登录后才可以留言!