1. The Filter is introduced

The Filter concept should be familiar to you, especially to those who started with servlets to learn the Java background. So what can we do with this thing? The Filter Filter is mainly used to Filter user requests. It allows pre-processing and post-processing of user requests, such as urL-level permission control and filtering out illegal requests. Filters are aspect oriented programming — a concrete implementation of AOP (AOP aspect programming is just a programming idea). In addition, the Filter is dependent on the Servlet container, and the Filter interface is under the Servlet package and is part of the Servlet specification. For this reason, we often refer to them as “enhanced servlets.” If we need to customize the Filter, it is very easy to implement the Javax.servlet. Filter interface and override the three methods in it! Filter.java

Public interface Filter {// Operations performed after initializing filters default void init(FilterConfig FilterConfig) throws ServletException {} // Void doFilter(ServletRequest Var1, ServletResponse Var2, FilterChain Var3) throws IOException, ServletException;  Void destroy() {}} Default void destroy() {}}Copy the code

2. How does Filter implement interception?

The Filter interface has a method called doFilter, which implements filtering of user requests. The specific process is like this:

  1. The user sends a request to the Web server, and the request goes to the filter first;
  2. The filter performs some processing on the request, such as filtering the parameters of the request, modifying the content of the response returned to the client, deciding whether to allow the user to access the interface, and so on.
  3. User request response complete.
  4. Do whatever else you want.

! [](https://img-blog.csdnimg.cn/img_convert/92c4b704fade4da1d47d8ea86f03a794.png#align=left&display=inline&height=394&mar gin=[object Object]&originHeight=394&originWidth=647&size=0&status=done&style=none&width=647)

3. How do I customize Filter

3.1 Practice javax.servlet.filter interface

@Component public class MyFilter1 implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException {system.out.println (" initialization method: executed only once when the server starts initialization "); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain FilterChain) throws IOException, ServletException {system.out. println(" Perform filter operation "); / * release operation * / filterChain doFilter (servletRequest, servletResponse); / / system.out.println (" this is done, echo back to client "); } /* Override public void destroy() {system.out.println (" destroy method, only once when the server is down "); }}Copy the code

3.2 Registering a user-defined filter in configuration

@Configuration public class MyFilterConfig { @Autowired MyFilter1 myFilter1; @Bean public FilterRegistrationBean<MyFilter1> Filter1(){ FilterRegistrationBean<MyFilter1> myFilter1FilterRegistrationBean = new FilterRegistrationBean<>(); / * set Filter * / myFilter1FilterRegistrationBean setFilter (myFilter1); / * * / set access path myFilter1FilterRegistrationBean. SetUrlPatterns (new ArrayList < > (arrays.aslist ("/filter / * "))); return myFilter1FilterRegistrationBean; }}Copy the code

3.3 Configuration Through Annotations

Note: ** to use WebFilter, you need to annotate @ServletComponentScan on the startup class.

@WebFilter(filterName = "MyFilter1",urlPatterns = "/filter") public class MyFilter1 implements Filter { @Override public Void init(FilterConfig FilterConfig) throws ServletException {system.out.println (" Initialization method: executes only once when the server starts initialization "); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain FilterChain) throws IOException, ServletException {system.out. println(" Perform filter operation "); / * release operation * / filterChain doFilter (servletRequest, servletResponse); / / system.out.println (" this is done, echo back to client "); } /* Override public void destroy() {system.out.println (" destroy method, only once when the server is down "); }}Copy the code

4. Customize multiple filters and determine the filter execution sequence

This is done by setting the filter level, calling the setOrder method of the FilterRegistrationBean

package com.pjh.Config; import com.pjh.Filter.MyFilter1; import com.pjh.Filter.MyFilter2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; import java.util.Arrays; /** * @ClassName: MyFilterConfig * @Author: 86151 * @Date: 2021/4/14 14:29 * @Description: TODO */ @Configuration public class MyFilterConfig { @Autowired MyFilter1 myFilter1; @Autowired MyFilter2 myFilter2; @Bean public FilterRegistrationBean<MyFilter1> Filter1(){ FilterRegistrationBean<MyFilter1> myFilter1FilterRegistrationBean = new FilterRegistrationBean<>(); / * set Filter * / myFilter1FilterRegistrationBean setFilter (myFilter1); Set filter levels / * * / myFilter1FilterRegistrationBean setOrder (1); / * * / set access path myFilter1FilterRegistrationBean. SetUrlPatterns (new ArrayList < > (arrays.aslist ("/filter / * "))); return myFilter1FilterRegistrationBean; } @Bean public FilterRegistrationBean<MyFilter2> Filter2(){ FilterRegistrationBean<MyFilter2> myFilter1FilterRegistrationBean = new FilterRegistrationBean<>(); / * set Filter * / myFilter1FilterRegistrationBean setFilter (myFilter2); Set filter levels / * * / myFilter1FilterRegistrationBean setOrder (2); / * * / set access path myFilter1FilterRegistrationBean. SetUrlPatterns (new ArrayList < > (arrays.aslist ("/filter / * "))); return myFilter1FilterRegistrationBean; }}Copy the code

5. Introduction of relevant notes

@WebFilter

An overview of the

@webFilter is used to declare a class as a filter. This annotation will be processed by the container at deployment time, and the container will deploy the corresponding class as a filter based on the specific property configuration. This annotation has some of the common attributes given in the table below. (All of the following attributes are optional, but value, urlPatterns, and servletNames must contain at least one of them. Value and urlPatterns cannot coexist. Value is usually ignored.

Common attributes of @webfilter

! [] (img – blog. Csdnimg. Cn/img_convert… Object]&originHeight=448&originWidth=866&size=0&status=done&style=none&width=866)

Matters needing attention

The first letter of filterName must be lowercase!! Lowercase!! Lowercase!!Copy the code
The Application startup class adds the @ServletComponentScan annotationCopy the code

@Order

An overview of the

The @ORDER or interface Ordered annotation defines the priority of the execution Order of beans in the Spring IOC container, not the load Order of beans. The load Order of beans is not affected by the @Order or Ordered interface.

Code implementation

@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD, Elementtype. FIELD}) @documented public @interface Order {/** * default is the lowest priority. A smaller value indicates a higher priority Ordered.LOWEST_PRECEDENCE; }Copy the code