SpringBoot中使用Filter和Interceptor的示例代码
一、Filter(过滤器)
Filter接口定义在javax.servlet包中,是Servlet规范定义的,作用于Request/Response前后,被Servlet容器调用,当Filter被Sring管理后可以使用Spring容器资源。
实现一个Filter
自定义的过滤器需要实现javax.servlet.Filter,Filter接口中有三个方法:
- init(FilterConfig filterConfig):过滤器初始化的被调用。
- doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain):在doFilter()方法中,chain.doFilter()前的一般是对request执行的过滤操作,chain.doFilter后面的代码一般是对response执行的操作,chain.doFiter()执行下一个过滤器或者业务处理器。
- destory():过滤器销毁的时候被调用。
在Spring容器中使用过滤器
通过FilterRegistrationBean
@Configuration public class WebConfig{ @Bean public FilterRegistrationBean xxxFilter() { FilterRegistrationBean registrationBean = new FilterRegistrationBean(); registrationBean.setFilter(new xxxFilter()); registrationBean.setUrlPatterns(Arrays.asList("/*")); registrationBean.setOrder(1); // 过滤器的优先级 return registrationBean; } }
通过@WebFilter和@ServletComponentScan
通过@WebFilter的方式定义Filter,默认使用Filter的类名设置优先级。使用FilterRegistrationBean可以指定优先级。Filter使用白名单过滤Url的方式,配置需要拦截的Url,如果想设置不过滤某些Url需要在doFilter方法中指定。
二、Interceptor(拦截器)
定义一个Interceptor需要实现org.springframework.web.servlet.HandlerInterceptor接口,Interceptor是Spring容器定义的,它可以使用Spring容器的任何资源,只要通过IoC注入到Interceptor即可,Interceptor可以深入到业务处理方法的执行前后和抛出异常的时候,而Filerter无法做到这一点,所以Interceptor相比Filter具有更大的弹性。
实现一个Interceptor
实现HandlerInterceptor或者继承HandlerInterceptorAdapter
public interface HandlerInterceptor { default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return true; } default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { } default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { } }
- preHandle:在业务处理器处理请求之前被调用。
- postHandle:在业务处理器处理请求后并生成视图前被调用,此时有机会修改ModelAndView。
- afterCompletion:业务处理处理器处理完请求后(已经渲染视图)被执行,并可以处理业务方法发生异常的场景。
在Spring容器中使用拦截器
@Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { InterceptorRegistration registration = registry.addInterceptor(new TimeInterceptor()); registration.excludePathPatterns("/user"); registration.excludePathPatterns("/*"); } }
Interceptor既可以指定要过滤的Url也可以指定不拦截的Url,缺省情况拦截所有Url。
三、调用顺序
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- Springboot中使用缓存的示例代码
- 使用Spring Boot创建Web应用程序的示例代码
- Spring Boot中使用RSocket的示例代码
- 使用Spring Boot+MyBatis框架做查询操作的示例代码
- Spring Boot中使用 Spring Security 构建权限系统的示例代码
- 使用Spring Boot集成FastDFS的示例代码
- SpringBoot中@WebFilter 过滤器(filter)和拦截器(Interceptor)的使用以及区别
- Spring Boot使用模板freemarker的示例代码
- Spring Boot中Servlet&Filter&Listener&Interceptor的使用
- Spring Boot2.0使用Spring Security的示例代码
- spring boot的拦截器简单使用示例代码
- 详解Spring Boot 使用Java代码创建Bean并注册到Spring中
- springboot扫描自定义的servlet和filter代码详解
- 41. Spring Boot 使用Java代码创建Bean并注册到Spring中【从零开始学Spring Boot】
- SpringBoot配置发送Email的示例代码
- SpringBoot使用自定义注解实现权限拦截的示例
- spring boot项目中使用spring-boot-devtools模块进行代码热部署,避免重新启动web项目
- maven工程使用spring-boot-devtools进行热部署,更改代码避免重启web容器
- Spring Boot定时任务的使用实例代码
- Spring Boot实现邮件注册功能示例代码