Details of the use of filter in springboot

  • 2020-11-25 07:18:48
  • OfStack

1. In the application of spring, there are two filtering usages, one is interceptor, and the other is filter of course. Here we describe the use of filters in springboot. The use of filters in springmvc is basically the same, but the configuration is a little different.

2. filter function, which enables users to change 1 request and modify 1 response. Filter is not 1 servlet, it cannot produce 1 response, it can preprocess request before 1 request arrives at servlet, and it can also process response when leaving servlet. In other words,filter is an servlet chaining (servlet chain).

1 Filter includes:

1) Interception before servlet is called;
2) Check servlet request before servlet is called;
3) Modify request header and request data as required;
4) Modify response header and response data as required;
5) Intercepted after servlet was called.

1. Application scenarios of Filter

Based on the understanding of THE filter filter, it can be done in the following three cases:

1 > Determines whether the target resource needs to be accessed by controlling the invocation of the chain.doFilter method.

For example, you can authenticate with user permissions and so on. Determines whether the user has access to certain resources, has permission to release, and does not execute the chain.doFilter method without permission.

2 > You do this by doing some processing before calling the chain.doFilter method.

For example, solve the problem of Chinese random code and so on. The encoding of the request and response can be set before the doFilter method. Even the request interface can be wrapped and decorated to handle the Chinese gibberish of the get request mode (override the corresponding ES67en.getParameter method).

3 > You do this by doing some processing after calling the chain.doFilter method.

For example, compress the entire web website. The response object is decorated with class A, overriding getOutputStream and overriding getWriter before calling the chain.doFilter method. Within class A, the output is cached into the ByteArrayOutputStream stream, and then the ByteArrayOutputStream stream cache data from class A is retrieved after the execution of the method chain.doFilter and compressed using the GZIPOutputStream stream.

2. Principle of Filter to achieve interception

There is one doFilter method in the Filter interface. After the developer has written the Filter class to implement the doFilter method and configured which web resource to intercept, the WEB server will call the doFilter method under 1 every time before calling the service method of web resource (determined by the server's internal access mechanism to the resource).

3. Filtering rules


 // Filters all resources in the application , All files under the current application root include all files under multiple subdirectories, note here * Before" / " 
 registration.addUrlPatterns("/*");
 // Filters the specified type file resource ,  All under the current application root directory html Document, note: *.html There is no "before / " , Otherwise the error 
 registration.addUrlPatterns(".html");
 // Filters all files in the specified directory , Under the current application root directory folder_name All files under subdirectories (which can be multilevel subdirectories) 
 registration.addUrlPatterns("/folder_name/*");
 // Filter the specified file 
 registration.addUrlPatterns("/index.html");

3. The application of


@Component
@ServletComponentScan
@WebFilter(urlPatterns = "/login/*",filterName = "loginFilter")
public class LoginFilter implements Filter{

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {

  }

  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

  }

  @Override
  public void destroy() {

  }
}

4. :

1. The purpose of the @Component annotation is to give LoginFilter to the container for processing. So let's make LoginFilter work

2. Use @ServletComponentScan to scan @WebFilter to make @WebFilter work. The servlet line pipe annotation is also available. This @ES125en would be a good idea to uninstall Apllication on top of this, general configuration. I didn't write it on Application because I only have one Filter.

3. The use of @WebFilter is obvious, for what links to do filtering, filter name is why.

5. Briefly introduce the usage of Filter in springmvc below

1, write methods or 1 kind are inherited from Filter, to achieve the three method processing

2. Drop into container: This needs to be configured in ES142en. xml


  <filter>
    <filter-name>loginFilter</filter-name>
    <filter-class>com.troy.boot.filter.LoginFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>loginFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

3, specific usage can be their own research.


Related articles: