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.
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() {
}
}
@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;
}
}
Plus directly on Test1Filter class@WebFilter(urlPatterns = "/*", filterName = "Test1Filter1") Registration
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 {
}
}
@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new Test1CostInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}
}
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 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...
table of Contents Application scenario Environmental preparation filter Listener Interceptor Link description operation result Filter and interceptor comparison Application scenario In the development...
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 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...
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 ...
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...
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...
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 ...
Static resource configuration YML mode, simple shortcut Configuration classes: Static resource mapping can be configured flexible; WebMvcConfigurer : Spring interface configuration class, FAQ configur...