两种方式:
- 通过注解扫描完成 Filter 组件的注册
- 通过方法完成 Filter 组件的注册
一、通过注解扫描完成 Filter 组件的注册
1. 编写Filter类
/** * SpringBoot整合Filter方式一 * * 传统方式 * <filter> * <filter-name>FirstFilter</filter-name> * <filter-class>com.linhw.demo.filter.FirstFilter</filter-class> * </filter> * * <filter-mapping> * <filter-name>FirstFilter</filter-name> * <url-pattern>/filter</url-pattern> * </filter-mapping> */ @WebFilter(filterName="FirstFilter", urlPatterns={"/filter"}) public class FirstFilter implements Filter{ @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { System.out.println("FirstFilter 进入......"); arg2.doFilter(arg0, arg1); System.out.println("FirstFilter 离开......"); } @Override public void destroy() { } }
2. 编写启动类
@SpringBootApplication @ServletComponentScan //扫描,并实例化类 public class App3 { public static void main(String[] args) { SpringApplication.run(App3.class, args); } }
二、通过方法完成 Filter 组件的注册
原文地址:https://www.cnblogs.com/myitnews/p/11518024.html
时间: 2024-09-30 04:40:32