Guest officer, there are filters and interceptors. Which one do you want?

What’s the difference between the two?



The resources

The difference between interceptors and filters

How do you get an interceptor?

1. First, write an interceptor

public class UrlInterceptor extends HandlerInterceptorAdapter {
    /** * Call before request processing (before Controller method call) * URL-based interceptor *@param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String path = request.getServletPath();
        if (。。。) {// If the address looks like this
            // So you do this
            return true;
        } else {// If the address is so and so
        	// You do this
            // This describes what you need to do to intercept, such as fetching cache, SESSION, permissions, etc
            System.out.println("= = >");
            // And so onResponse. sendRedirect(other address);// Do it again
            return false;// Jump should return false
        }
        // Do you understand?}}Copy the code

2. The Spring Boot project registers the interceptor

@Configuration
public class WebConfigurer implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(UrlInterceptor()).addPathPatterns("/ * *");
    }

    @Bean
    public UrlInterceptor UrlInterceptor(a) {
        return newUrlInterceptor(); }}Copy the code

@Configuration This annotation will automatically register us.

Gunning to bask in.