One, in the application of spring we have two kinds of filter usage, one is the interceptor, and the other is of course a filter. Here we introduce the use of filters in the Springboot, the usage in SPRINGMVC is basically the same, just the configuration above a bit different.
Second, the filter function, it allows users to change a request and modify a response. Filter is not a servlet, it cannot produce a response, it can preprocess the request before a request arrives at the servlet, or it can handle response when it leaves the servlet. In other words, filter is actually a "Servlet Chaining" (servlet chain).
A filter includes:
1), intercepted before the servlet is invoked;
2), check the servlet request before the servlet is invoked;
3), modify the request header and request data as required;
4), modify response head and response data as needed;
5), intercepted after the servlet is invoked.
Third, the application
@Component @servletcomponentscan@webfilter (urlpatterns= "/login/*", FilterName = "Loginfilter") Public classLoginfilterImplementsfilter{@Override Public voidInit (Filterconfig filterconfig)throwsservletexception {} @Override Public voidDoFilter (ServletRequest servletrequest, Servletresponse servletresponse, Filterchain filterchain)throwsIOException, servletexception {} @Override Public voiddestroy () {}}
Iv. Explanation:
1, @Component The purpose of this annotation is to give loginfilter to the container to handle. Which means that loginfilter works.
2, @ServletComponentScan This use to scan @webfilter let @webfilter function. Of course, the servlet line tube annotations are also possible. This @servletcomponentscan best uninstall apllication This above, universal configuration. I do not write it on application because there is only one filter.
3, @WebFilter This use is obvious, for what link to do filter, filter name is why.
V. Brief introduction to the use of the filter in SPRINGMVC
1, write the method or the same is inherited filter, to achieve 3 methods of processing
2. Drop into 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, the specific usage can be researched by oneself.
Usage of filter in Springboot