Reprinted from Springboot use Filter and tread pit www.cnblogs.com/javafucker/…
There are two ways to use filters in Springboot: annotating and registering beans
First, annotation @webfilter
1. Implement Filter interface (Javax.servlet)
2. Add @webFilter annotations
3. Add the @ServletComponentScan annotation to the startup class
Attached code:
First Filter:
@Slf4j @WebFilter(filterName = "filter1", urlPatterns = {"/url1/*"}) public class Filter1_Filter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { log.info("do filter1"); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } }Copy the code
Second Filter:
@Slf4j @WebFilter(filterName = "filter2", urlPatterns = {"/url2/*"}) public class Filter2_Filter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { log.info("do filter2"); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } }Copy the code
Start the class:
@SpringBootApplication @ServletComponentScan public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication .class, args); }}Copy the code
Here are the pits I’ve stepped in
1. Filter cannot be injected without @ServletComponentScan annotation. The purpose of this annotation is to scan @webServlet, @webFilter, @WebListener and other special annotations. If not, the Spring container will not actively scan
The order in which a Filter is executed is controlled by the first letter of the Filter class name. For example, Filter1_Filter is executed before Filter2_Filter. However, some online materials say that there is no clear execution order when there are a large number of filters, so this method should be abandoned and the second method is recommended
Register beans
1. Implement the Filter interface
2. Register Filter
Here is the code attached:
First Filter:
@Slf4j
public class Filter1 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
log.info("do filter1");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
Copy the code
Second Filter:
@Slf4j
public class Filter2 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
log.info("do filter2");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
Copy the code
Registered:
@Configuration public class FilterConfig { @Bean public Filter filter1(){ return new Filter1(); } @Bean public Filter filter2(){ return new Filter2(); } @Bean public FilterRegistrationBean setFilter1() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(filter1()); filterRegistrationBean.addUrlPatterns("/url1/*"); filterRegistrationBean.setOrder(1); // The lower the value of order, the higher the priority among all filters. } @Bean public FilterRegistrationBean setFilter2(){ FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(filter2()); filterRegistrationBean.addUrlPatterns("/url2/*"); filterRegistrationBean.setOrder(2); // The lower the value of order, the higher the priority among all filters. }}Copy the code
This way is more rigorous, through filterRegistrationBean setOrder (1) set up effective execution order close test