Usage of filter in Springboot

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.