Spring boot filter, interceptor

tags: Development Framework

Filter FILTER is part of the servlet container, part of the Servlet specification,
The interceptor is independent, which can be used in any case.

Filter filter

  • There are two ways to implement filter
    1. Register via FilterRegistrationBean instance
    2. Take effect by @Webfilter (cannot set priority between filters)

FilterRegistrationBean method registration

1. Implement a Filter interface, write a filter

public class Test1Filter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    
    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
            throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)arg0;
        System.out.println("Custom filter Filter1 trigger, intercept URL:"+request.getRequestURI());
        arg2.doFilter(arg0,arg1);
    }
    
    @Override
    public void destroy() {
    }
}

2. Register the filter

FilterRegistrationBean instance registration filter

@Configuration
public class testFilterConfig {

    @Bean
    public FilterRegistrationBean<Test1Filter> RegistTest1(){
        
        FilterRegistrationBean<Test1Filter> bean = new FilterRegistrationBean<Test1Filter>();
        bean.setFilter(new Test1Filter());// Register a custom filter
        bean.setName("flilter1");// Filter name
        bean.addUrlPatterns("/*");// Filter all paths
        // Setting the priority via the FilterRegistrationBean instance can take effect through @Webfilter invalid
        bean.setOrder(1);// Priority, the top
        return bean;
    }
}

@Webfilter Note Registered Filter

Plus directly on Test1Filter class@WebFilter(urlPatterns = "/*", filterName = "Test1Filter1") Registration

  1. @Webfilter This annotation is the specification of servlet3.0, not Spring Boot provided by Spring Boot.
  2. @WebFilter This annotation does not specify the properties of the execution order, which depends on the name of the Filter, is based on the Filter class name (note that the name is not the configured filter) alphabetical alphabetical alphabetically
  3. Filter priority is higher than FilterRegistrationBean configured filter

Interceptor interceptor

Implement the HandlerInterceptor interface, write interceptors

public class Test1CostInterceptor implements HandlerInterceptor {
    long start = System.currentTimeMillis();
    
    / / Request before execution
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("Customized interceptor trigger, request start");
        return true;
    }
 
 	/ / Request end execution
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("Custom interceptor trigger, request end");
    }
 
 	/ / After the view rendering is completed
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}

Registered interceptor

@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter {
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new Test1CostInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
}

Intelligent Recommendation

spring boot filter, interceptor related

Interceptors and filters Some reprinted fromSpring Boot uses filter Filter - Jianshu Filters are filtering and preprocessing processes for data. When we visit the website, we sometimes publish some se...

Spring boot interceptor and shiro filter Filter

Spring boot interceptor interceptor is executed in the servlet, Shiro should be Filter, execute before servlet. Springmvc interceptor is a priority below Shiro, Shiro is a Filter that is customized fo...

Spring boot learning --- filter listener interceptor

table of Contents Application scenario Environmental preparation filter Listener Interceptor Link description operation result Filter and interceptor comparison Application scenario In the development...

Simple configuration of the Spring Boot interceptor Filter

Spring Boot adds filtering Method 1: Annotation Method 2: Register the Bean with the FilterRegistrationBean Spring Boot adds Filter in two ways: annotating and registering beans using the FilterRegist...

spring boot (2) servlet listener filter interceptor

spring boot 2 servlet listenerfilter interceptor   Generally for web development, using the controller can meet most of the needs, but sometimes the servlet listener filter and interceptor are al...

More Recommendation

5-Spring Boot (filter, listener, interceptor)

original: 1. Filter: Create ServletFilter.java to implement the Filter method test: Console output: 2. Listener: Create ContextListener.java class When the project starts, the listener is initialized ...

spring boot (7) listener, filter and interceptor

2019 Unicorn Enterprise Heavy Recruitment Standards for Python Engineers >>> 1 Listeners, filters and interceptors 1.1 Listener Listener, it is a server-side program that implements the javax...

Spring boot (5) Filter / Interceptor / Listener

When using Spring-Boot, embedded servlet containers register all listeners (such as HttpSessionListener listeners) that servlet, Filter, and Servlet specification by scanning. Spring boot's main servl...

Spring boot filter, listener and interceptor use

1, filters and listeners Spring boot is nothing different from the usual web engineering in the general web project, using annotations can be created quickly, just need to use annotations, need to be ...

Spring boot static resource configuration; interceptor, filter

Static resource configuration YML mode, simple shortcut Configuration classes: Static resource mapping can be configured flexible; WebMvcConfigurer : Spring interface configuration class, FAQ configur...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top