Knowledge points:
1, SpringBoot2.x filter Filter and use Servlet3.0 configure a custom Filter (core knowledge)
filter is simple to understand: people ---> tellers (filter) ---> Attractions
1) SpringBoot default boot loader of 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 it will conflict
Filter the bean FilterRegistrationBean registered
with the relevant module which has a default Filter
WEB-> servlet-> filter
3) Custom Filter
A) used Servlet3.0 annotations configured
b) a promoter which increases class @ServletComponentScan, scan
c) a new Filter class, implements Filter, and to implement the corresponding interfaces
d) @WebFilter labeled as a filter class , the spring scanned
urlPatterns: blocking rules, regular support
e) The method of controlling chain.doFilter calls, 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
4) official website address: https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-embedded-container-servlets-filters-listeners
2, Servlet3.0 annotation custom native Servlet combat
explain: using custom annotations Servlet3.0 Servlet and native The Listener
. 1) custom native Servlet
@WebServlet(name = "userServlet",urlPatterns = "/test/customs") public class UserServlet extends HttpServlet{ @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().print("custom sevlet"); resp.getWriter().flush(); resp.getWriter().close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
3, Servlet3.0 comments custom native Listener listener combat
Introduction: Introduction and Servlet3.0 listener comments custom native Listener listener combat
1) Custom The Listener (common listener servletContextListener, httpSessionListener, servletRequestListener)
@WebListener public class RequestListener implements ServletRequestListener { @Override public void requestDestroyed(ServletRequestEvent sre) { // TODO Auto-generated method stub System.out.println("======requestDestroyed========"); } @Override public void requestInitialized(ServletRequestEvent sre) { System.out.println("======requestInitialized========"); }
4, SpringBoot2.X custom interceptor combat and the old and new configuration comparison (Core Knowledge)
Description: A tutorial on the use of interceptors, Spingboot2.x new version configured interceptor interceptors and interceptor old version SpringBoot configuration to explain the difference between
1) @Configuration
inherited WebMvcConfigurationAdapter ( SpringBoot2.X before the old version)
SpringBoot2.X new version configured interceptor implements WebMvcConfigurer
2) custom interceptor HandlerInterceptor
preHandle: Controller before calling a method
postHandle: After Controller calls, before rendering a view, if the Controller appeared abnormal, this method is not performed
afterCompletion: Whether or not an exception, this will be afterCompletion It calls for resources to clean up
3) to intercept registered in accordance with the order to register, first to be intercepted
Interceptor does not take effect Frequently Asked Questions:
A) Are there plus @Configuration
b) whether there is a path to intercept and question ** *
c) interceptor last path must be "/ *", if it is, then the directory is / * /
Filter
is based on the doFilter callback (), and is based AOP Interceptor thought
Filter only work in front Servlet, Interceptor and deep enough to methods before and after, before and after the exception is thrown, etc.
depend on the web application Servlet Container i.e., without the Interceptor depends on the Servlet containers can be run in a variety of environments.
Interface called life cycle, Interceptor can be called multiple times, and Filter can only be called once when the container is initialized.
Filter Interceptor and the order of execution
before filtration -> front knockdown -> Action Execution -> intercepting -> filtration