本文最后更新于2 分钟前,文中所描述的信息可能已发生改变。
二者对比(其实作用都差不多)
Interceptor | Filter | |
---|---|---|
技术范畴 | SpringMVC框架技术 | javaweb(servlet)技术 |
拦截/过滤的资源 | 进入了SpringMVC管辖范围的,主要是一个个controller中的方法 | 任何请求与资源 |
执行时机 | 早于任何Servlet执行 | DispatchServlet之后controller之前执行 |
作用范围
执行流程
自定义Interceptor
Interceptor类似于使用aop对方法进行增强。要自定义Interceptor需要实现HandlerInterceptor接口
java
@Component
@Slf4j
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("controller处理前.....此方法返回true请求才会被controller处理," +
"返回false则直接跳到前一个interceptor的afterCompletion方法处");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
log.info("controller处理后.....");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
log.info("视图渲染完成后,整个流程执行完毕.....");
}
}
注册自定义Interceptor
WebMvcConfigurationSupport
是 Spring MVC 框架提供的一个用于扩展和定制 MVC 配置的类。通过继承WebMvcConfigurationSupport
类,你可以自定义 Spring MVC 的配置,包括添加拦截器、消息转换器、视图解析器等
java
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
@Autowired
private MyInterceptor myInterceptor;
@Override
protected void addInterceptors(InterceptorRegistry registry) {
log.info("开始注册自定义拦截器...");
registry.addInterceptor(myInterceptor)
.addPathPatterns("/admin/**")
.excludePathPatterns("/admin/login");
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
log.info("配置静态资源映射..");
registry.addResourceHandler("/index.html").addResourceLocations("classpath:/resource/static/");
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters){
log.info("扩展消息转换器...");
//创建一个消息转换器
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//设置对象转换器,将java对象序列化为JSON数据
converter.setObjectMapper(new JacksonObjectMapper());
//将自定义消息转换器加入容器
converters.add(0,converter);
}
}
自定义Filter
通过实现Filter接口即可自定义Filter,可以对HttpServletRequest与HttpServletResponse做一些处理。
java
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("初始化filter参数中...");
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("过滤器过滤中.....");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
System.out.println("销毁方法执行中...");
Filter.super.destroy();
}
}
注册自定义Filter
通过FilterRegistrationBean即可注册自定义Filter并指定过滤规则。
java
@Configuration
public class WebConfig {
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>();
filterRegistrationBean.setFilter(new MyFilter());
filterRegistrationBean.setName("myFilter");
filterRegistrationBean.setOrder(0);
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
}