码迷,mamicode.com
首页 > 编程语言 > 详细

Springboot整合Filter

时间:2020-04-13 14:00:48      阅读:80      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!