1. In spring application, we have two uses of filtering, one is an interceptor, and the other is of course a filter. We will introduce the usage of filters in springboot here. The usage of filters in springmvc is basically the same, but there is a little difference in configuration.
2. The filter function allows users to change a request and modify a response. Filter is not a servlet, it cannot generate a response. It can preprocess the request before a request reaches the servlet, or it can handle the response when leaving the servlet. In other words, filter is actually a "servlet chaining" (servlet chain).
A Filter includes:
1) Intercept before the servlet is called;
2) Check the servlet request before the servlet is called;
3) Modify the request header and request data as needed;
4) Modify the response header and response data as needed;
5) Intercepted after the servlet is called.
1. Application scenarios of Filter
Through the understanding of filter filters, we can know that some processing can be done in the following three situations:
1> Decide whether the target resource needs to be accessed by controlling the call to the chain.doFilter method.
For example, you can verify in user permissions, etc. Determine whether the user has permission to access certain resources, has permission to release, and does not execute the chain.doFilter method without permission.
2> Some purposes are achieved by doing some processing before calling the chain.doFilter method.
For example, solve the problem of Chinese garbled code, etc. Before the doFilter method, the encoding of the setting request encoding and response can be performed. You can even encapsulate and decorate the request interface to handle the Chinese garbled problem of get request method (rewrite the corresponding request.getParameter method).
3> Some purposes are achieved by doing some processing after calling the chain.doFilter method.
For example, compress the entire web site. Before calling the chain.doFilter method, encapsulate and decorate the response object with class A, override getOutputStream and override getWriter method. Inside class A, the output content is cached into the ByteArrayOutputStream stream, and then after the chain.doFilter method is executed, the ByteArrayOutputStream stream cache data in class A is obtained and compressed with the GZIPOutputStream stream.
2. The principle of Filter interception
There is a doFilter method in the Filter interface. When the developer writes the Filter class to implement the doFilter method and configures which web resource to intercept, the WEB server will call the filter's doFilter method first before calling the service method of the web resource (determined by the server's internal access mechanism).
3. Filtering rules
//Filter all resources in the application. All files under the root of the current application include all files under the multi-level subdirectory. Note that * is preceded by "/" registration.addUrlPatterns("/*"); //Filter the specified type of file resources, all html files under the root of the current application. Note: There is no "/" before *.html, otherwise the error registration.addUrlPatterns(".html"); //Filter all files under the specified directory.addUrlPatterns("/index.html"); //Filter all files under the folder_name subdirectory (can be a multi-level subdirectory) under the root of the current application.addUrlPatterns("/folder_name/*"); //Filter the specified file registration.addUrlPatterns("/index.html");III. Application
@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. Explanation:
1. The purpose of this annotation @Component is to hand over the LoginFilter to the container for processing. That is to make LoginFilter work
2. @ServletComponentScan This is used to scan @WebFilter to make @WebFilter work. Of course, it is also possible to explain the servlet wire tube. This @ServletComponentScan is best to uninstall the Application, the general configuration. I have only one Filter here, so I didn't write it on the Application.
3. @WebFilter is of obvious use. What link is used to filter and why is the name of filter?
5. A brief introduction to the usage of Filter in springmvc
1. The writing method is still the same. It inherits Filter to implement 3 methods to process it.
2. Throw it into the container: This needs to be configured in web.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. You can study the specific usage by yourself.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.