This is the 22nd day of my participation in the First Challenge 2022

1. Filter

Filter is implemented through java.servlet. Filter interface. It is used for processing incoming request and response, such as checking request parameters, setting and checking header information, or checking some illegal behaviors. Based on the interface implemented, the filter is dependent on the servlet container. So since the filter doesn’t depend on the Spring container, it can’t get objects in the container.

Create a filter class that inherits the java.servlet.filter interface and implements the intercepting methods in filter, along with initialization and destruction methods. In the doFilter method, add the business logic for the specific execution method that needs to be intercepted.

public class MyFilter implements Filter {

	@Override
	public void init(FilterConfig fc) throws ServletException {}@Override
	public void doFilter(ServletRequest sr, ServletResponse sr, FilterChain fc) throws IOException, ServletException {}@Override
	public void destroy(a) {}}Copy the code
  1. Instantiate the custom filter, and instantiate the filter chain object, and then add the created filter class instantiated into the filter chain, add some related items after injection intoSpringIn the container.
@Bean
public FilterRegistrationBean filterRegistrationBean(a){
    // ...
    filterRegistrationBean.setFilter(myFilter());
}

@Bean
public Filter myFilter(a){
    return new MyFilter();
}
Copy the code

2. Interceptor

Interceptors Interceptor is by implementing org. Springframework. Web. HandlerInterceptor interface implementation servlet package, the interface is the spring container interfaces, so it is dependent on the spring container. The main function is the idea of AOP, which can crosscut a method and do some business logic.

1. Write a custom interceptor class for the HandlerInterceptor interface, and write both pre-processing, post-processing, and callback on completion of the call (request callback method after completion of the call, that is, after the view rendering is completed).

public class CustomHandlerInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest hr, HttpServletResponse hsr, Object o) throws Exception {
		return false;
	}

	@Override
	public void postHandle(HttpServletRequest hsr, HttpServletResponse hsrs, Object o, ModelAndView modelAndView) throws Exception {}@Override
	public void afterCompletion(HttpServletRequest r, HttpServletResponse h, Object o, Exception e) throws Exception {}}Copy the code

The WebMvcConfigurerAdapter is out of date in spring2.0. This is just for demonstration purposes. If you are interested, take a look at the WebMvcConfigurer implementation in spring2.0.

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(getHandlerInterceptor()).addPathPatterns("/ *");
	}

	@Bean
	public static HandlerInterceptor getHandlerInterceptor(a){
		return newCustomHandlerInterceptor(); }}Copy the code

Filter and interceptor execution process diagram

After executing each Filter#doFilter() method in the Filter chain, Servlet#service() method is entered. The dispatcher dispatcher then passes the request method to the corresponding mapped handler controller. Interceptor#preHandler() = Interceptor#preHandler() = Interceptor#preHandler(); After Interceptor#postHandler(), it intercepts the Controller method until the returned data model reaches the view parser. Then it Interceptor#afterCompletion(), It can mainly operate the logic before returning to the client, and finally return to the call point of each Filter in the Filter chain, and process the jump back to the client and other logic.

Third, summary

Filters are interfaces in servlets that can be used to intercept requests for HttpServletRequest and perform logical operations such as checking as needed before the request enters the servlet or before the HttpServletResponse is returned to the client.

Interceptor is an interface in Spring, so it can get some beans and other resources in Spring. It is widely used in section-oriented programming. Interception is actually an AOP strategy.