1. Create a new Filter class and implement the Filter interface.
@WebFilter(filterName = "abc", urlPatterns = "/*") public class MyFilter1 implements Filter
illustrate:
1. Since Servlet 3.0, Servlet, Filter, Listener do not need to be configured in web.xml, but can be configured through Annotation;
2. For details, please refer to: New Features of servlet 3.0
2. Add the annotation @ServletComponentScan to the entry of spring-boot
@ServletComponentScan
@SpringBootApplication
public class KkdemoApplication {
public static void main(String[] args) {
SpringApplication.run(KkdemoApplication.class, args);
}
}
illustrate:
@ServletComponentScan,Enables scanning for Servlet components ({WebFilter filters}, {WebServlet
* servlets}, and {WebListener listeners})
A description of the execution order when there are multiple filters:
1. When there are multiple Filters, if you need to specify the order of execution, you can indirectly implement it by annotating the filterName property of @WebFilter and specifying a name for the filter, because in spring-boot, the execution order of filters is executed according to the natural order of filterName by default. (For example: 1 has a priority greater than 2; 11 has a priority greater than 2; a has a priority greater than b).
2、经测试,Fliter类上加@Order(int seq) 注解无效(测试环境:spring-core-4.3.13.RELEASE)