• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot中使用Filter和Interceptor的示例代码

springboot 搞代码 4年前 (2022-01-05) 17次浏览 已收录 0个评论

这篇文章主要介绍了SpringBoot中使用Filter和Interceptor的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、Filter(过滤器)

Filter接口定义在javax.servlet包中,是Servlet规范

来源gao!%daima.com搞$代*!码网

定义的,作用于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。

三、调用顺序

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是SpringBoot中使用Filter和Interceptor的示例代码的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot中使用Filter和Interceptor的示例代码

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址