Example code of springboot filter and interceptor

Time:2022-1-15

Interceptors and filters

Before we talk about spring boot, let’s take a look at filters and interceptors. The two are similar in function, but there is still a big gap in specific technical implementation. Before analyzing the difference between the two, let’s first understand the concept of AOP. AOP is not a specific technology, but a programming idea. In the process of object-oriented programming, we can easily solve the vertical expansion through inheritance and polymorphism. However, for horizontal functions, such as opening transactions in all service methods or uniformly recording logs, object-oriented cannot be solved. Therefore, AOP — aspect oriented programming is actually a supplement to the idea of object-oriented programming. The filters and interceptors we are talking about today belong to the specific implementation of aspect oriented programming. The main differences between the two include the following aspects:

1. Filter depends on the servlet container and is a part of the servlet specification, while interceptor exists independently and can be used in any case.

2. The implementation of filter is completed by servlet container callback, and interceptor is usually executed by dynamic proxy.

3. The life cycle of the filter is managed by the servlet container, while the interceptor can be managed through the IOC container. Therefore, you can obtain instances of other beans through injection, so it will be more convenient to use.

filter


@Component
public class AddResponseHeaderFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        String header = response.getHeader("Set-Cookie");
        Collection<String> headerNames = response.getHeaderNames();
        System.out.println("___________________");
        System.out.println(header);
        System.out.println("___________________");
        filterChain.doFilter(request, response);
    }
}

Interceptor

public class HttpResponseInterceptorHandler implements HandlerInterceptor {
    //You can implement handlerinterceptor or inherit handlerinterceptoradapter. If you want to look concise, use the latter
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.addHeader("content-test","123");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}

This is the end of this article about springboot filters and interceptors. For more information about springboot filters and interceptors, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!