1,什么是过滤器拦截器?
拦截器是SpringMVC的技术
过滤器的Servlet的技术
先过过滤器,过滤器过完才到DispatcherServlet,由拦截器进行拦截;
2,执行顺序
类似于栈,先进后出
3,代码实现
【过滤器执行逻辑】
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(filterName = "MyFilter", urlPatterns = "/*")
public class MyFilter implements Filter {
@Override
public void init(javax.servlet.FilterConfig filterConfig) throws ServletException {
System.out.println("TestInter1 filter init");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("TestInter1 filter before ...");
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("TestInter1 filter after ...");
}
@Override
public void destroy() {
System.out.println("TestInter1 filter destroy");
}
}
【过滤器配置】
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean testFilter1() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
MyFilter testFilter1 = new MyFilter();
filterRegistrationBean.setFilter(testFilter1);
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.setName("testFilter1");
filterRegistrationBean.setOrder(2);//指定过滤顺序,数值越小优先级越高
return filterRegistrationBean;
}
}
【拦截器执行逻辑】
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("TestInter1 Interceptor preHandle...");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("TestInter1 Interceptor postHandle...");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("TestInter1 Interceptor afterCompletion...");
}
}
【拦截器注册】
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Autowired
private MyInterceptor testInter1;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//默认/** 拦截所有请求
registry.addInterceptor(testInter1).addPathPatterns("/**");
}
}
4,代码运行输出顺序
过滤器-拦截器-过滤器
//项目启动时
过滤器初始化 TestInter1 filter init
//开始发送请求
过滤器执行前 TestFilter1 filter before ...
拦截器操作前 TestInter1 Interceptor preHandle...
拦截器操作中 TestInter1 Interceptor postHandle...
拦截器操作后 TestInter1 Interceptor afterCompletion...
过滤器执行后 TestFilter1 filter after ...
5,好文推荐
https://www.yht7.com/news/186389