notes
1, depth filter Filter SpringBoot2.x and use Servlet3.0 configure a custom Filter combat (Core Knowledge)
Description: Filter explain SpringBoot inside and explain the use of Servlet3.0 configure custom Filter combat
filter is simple to understand: people ---> ticket members (filter) ---> attractions
1, SpringBoot start loaded by default the filter
characterEncodingFilter
hiddenHttpMethodFilter
httpPutFormContentFilter
RequestContextFilter
2, the filter priority
Ordered.HIGHEST_PRECEDENCE
Ordered.LOWEST_PRECEDENCE
low value means a higher priority higher values are interpreted as lower priority
custom Filter, Filter avoid default priority, as otherwise the conflict will
register Filter of bean FilterRegistrationBean
with the relevant module which has a default Filter
WEB-> servlet-> filter
3, custom Filter
. 1) to be configured using annotations Servlet3.0
2) which increases the startup class @ServletComponentScan, scan
3) Create a Filter class, implements Filter, and to implement the corresponding interface
4) @WebFilter mark a class for the filter, is to scan the Spring
urlPatterns: blocking rules, supporting regular
6) calling methods of control chain.doFilter to achieve by whether the release
is not released, web applications resp.sendRedirect ( "/ index.html" );
scene: access control, user login (non isolated distal rear scene) or the like
1, official website address: https: //docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle / # boot-features-embedded- container-servlets-filters-listeners
Start
Start the application. View the boot log
which is the default filter springboot the
mapping is mapped. Including our own to write
dynamically load
exception handling ControllerAdvice
the Ordered is an interface.
There are two constants. Integer The maximum and minimum values
there is also a method geteOrder its priority is acquired
here view FilterRegistrationBean. View point into the source code. FilterRegistrationBean This is a registered Filter of Bean
in springboot package
These are the default Filter
not open a default Filter
returns above order of priority by getOrder method
to see him again on an
upper level, which has a more important knowledge points. doFilter, we custom filters will explain when doFilter
filter must rely Servlet container, the main mechanism is based on a callback mechanism
Custom Filter
Servlet3.0 annotations using our custom configuration Filter
3, custom Filter
. 1) are configured using annotations Servlet3.0
2) which increases the startup class @ServletComponentScan, scan
3) Create a Filter class, implements Filter, and achieve corresponding interface
4) @WebFilter labeled as a class filter, is scanned spring
urlPatterns: blocking rules, regular support
6) of a control method call chain.doFilter, whether achieved by release
no release, web applications resp.sendRedirect ( " /index.html ");
scene: access control, user login (non isolated distal rear scene) or the like
built a Filter module
implemented Filter interface.
There is a need to implement a method which
init method is the time to start a container loaded
doFilter: approach to the core business of
xdclss equal to the received parameter username then release, otherwise direct return to return to
destory be called when the container destroyed.
Configuration notes, adding spring to manage
@WebFilter is a comment spring3.0 of.
urlPatterns: To intercept the url. / * Is to intercept all requests. Here are all the configuration / api / * interception api beginning of the following request.
Of course, we can also block such as a request under the different modules. This is coupled with the front admin intercepts all requests in the admin module.
filterName here less than being the first. Here just write a name on it.
4) @WebFilter labeled as a class filter, is scanned spring
urlPatterns: blocking rules, regular support
6) of a control method call chain.doFilter, whether achieved by release
no release, web applications resp.sendRedirect ( "/ index. html ");
scene: access control, user login (non isolated distal rear scene) or the like
Start class needs to add comment @ServletComponentScan
Start the test
View the boot log. Filter our custom has been initialized.
Several above is the default Filter. The following loginFilter is our custom
access interface test. Api will only be the beginning of the intercepted
access to this address. Page no output.
But backstage output.
We pass a username in the past. username equal xdclass be allowed to go.
Quickly create a new method to test
When the heat load, reload the container. Filter the destory method has been called.
When reloaded or initialized Filter
for a wrong username value. Nothing is returned
plus a jump. Then forced to return
static pages here
to change the username aaa
jump to the index page
address of the official website of the filter is described
1, the official website address: https: //docs.spring.io/spring-boot/docs/2.1.0. BUILD-SNAPSHOT / reference / htmlsingle / # boot-features-embedded-container-servlets-filters-listeners
new features Servlet3.0 of. It is not burned by touching the application of new releases. There is also asynchronous