首先在介绍springboot实现过滤器之前,我们先来了解下sevlet的三大组件: Spring Boot @ServletComponentScan 扫描 @WebServlet、@WebFilter、@WebListener Servlet 三大组件 Servlet、Filter、Listener 在传统项目中需要在 web.xml 中进行相应的配置。Servlet 3.0 开始在 javax.servlet.annotation 包下提供 3 个对应的 @WebServlet、@WebFilter、@WebListener 注解来简化操作 @WebServlet、@WebFilter、@WebListener 写在对应的 Servlet、Filter、Listener 类上作为标识,从而不需要在 web.xml 中进行配置了 Spring Boot 应用中这三个注解默认是不被扫描的,需要在项目启动类上添加 @ServletComponentScan 注解, 表示对 Servlet 组件扫描
Spring Boot 应用中这三个注解默认是不被扫描的,需要在项目启动类上添加 @ServletComponentScan 注解, 表示对 Servlet 组件扫描
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan //对 servlet 注解进行扫描 public class RedisStuWebApplication { public static void main(String[] args) { SpringApplication.run(RedisStuWebApplication.class, args); } }在介绍完sevlet的三大组件之后,我们再来学习如何实现过滤器就不会那么迷茫了……
使用@WebFilter注解为声明当前类为filter,第一个参数为该filter起一个名字,第二个参数为说明要拦截的请求地址,当前类需要实现Filter接口,里面有三个方法,分别为过滤器初始化、过滤方法和过滤器销毁
@Slf4j @WebFilter(filterName = "myFilter1", urlPatterns = "/*") public class MyFilter1 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { log.info(filterConfig.getFilterName() + " init"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { log.info("myFilter1 begin"); try { log.info("业务方法执行"); chain.doFilter(request, response); } catch (Exception e) { log.error("error!", e); } log.info("myFilter1 end"); } @Override public void destroy() { } }启动类添加@ServletComponentScan注解,@ServletComponentScan注解所扫描的包路径必须包含该Filter,代码如下: 之所以加这个注解,因为springboot默认不扫描Servlet的三大组件,所以需要加
@SpringBootApplication @ServletComponentScan(basePackages = "com.example.demo.filter") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }新建MyFilter2.java类,不要加注解@WebFilter,代码如下:
@Slf4j public class MyFilter2 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { log.info(filterConfig.getFilterName() + " init"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { log.info("myFilter2 begin"); try { log.info("业务方法执行"); chain.doFilter(request, response); } catch (Exception e) { log.error("error!", e); } log.info("myFilter2 end"); } @Override public void destroy() { } }新建配置类WebConfig.java,配置bean,代码如下:
@Configuration public class WebConfig { @Bean public FilterRegistrationBean testFilterRegistration() { //配置多个过滤规则 // List<String> urls = new ArrayList<>(); // urls.add("/order/*"); // urls.add("/user/*"); // registration.setUrlPatterns(urls); FilterRegistrationBean registration = new FilterRegistrationBean(new MyFilter2()); registration.addUrlPatterns("/test"); // registration.setName("myFilter2"); return registration; } }项目启动后浏览器访问http://localhost:8080/test,可以看到过滤器已生效,后台打印日志如下:
[nio-8080-exec-1] com.example.demo.filter.MyFilter1 : myFilter1 begin [nio-8080-exec-1] com.example.demo.filter.MyFilter1 : 业务方法执行 [nio-8080-exec-1] com.example.demo.filter.MyFilter1 : myFilter1 end到此Spring Boot配置过滤器的两种方式已经全部实现,有问题欢迎留言沟通哦!