加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

SpringBoot拦截器的使用小结

发布时间:2020-12-14 19:47:55 所属栏目:Java 来源:网络整理
导读:总结一下SpringBoot下拦截器的使用,步骤很简单: 1.自定义自己的拦截类,拦截类需要继承HandlerInterceptor接口并实现这个接口的方法。 @Override public boolean preHandle(HttpServletRequest httpServletRequest,HttpServletResponse httpServletRespons

总结一下SpringBoot下拦截器的使用,步骤很简单:

1.自定义自己的拦截类,拦截类需要继承HandlerInterceptor接口并实现这个接口的方法。

@Override
  public boolean preHandle(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,Object o) throws Exception {
    //方法调用前执行
    return true;//返回为false,拦截器拦截的方法不会调用
  }
  @Override
  public void postHandle(HttpServletRequest httpServletRequest,Object o,ModelAndView modelAndView) throws Exception {
  //方法执行结束后执行
  }
  @Override
  public void afterCompletion(HttpServletRequest httpServletRequest,Exception e) throws Exception {
  //该方法将在整个请求完成之后,也就是DispatcherServlet渲染了视图执行,这个方法的主要作用是用于清理资源的,
  }  

2.配置类需要继承WebMvcConfigurerAdapter类

 @Autowired
  private LoginInterceptor loginInterceptor;//自己定义的拦截器类
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
  registry.addInterceptor(loginInterceptor).addPathPatterns("拦截URL,可以不填默认全部请求拦截");
  }

3.启动SpringBoot应用即可。

以上所述是小编给大家介绍的SpringBoot拦截器的使用小结,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

您可能感兴趣的文章:

  • SpringBoot快速设置拦截器并实现权限验证的方法
  • 详解SpringBoot AOP 拦截器(Aspect注解方式)
  • SpringBoot定义过滤器、监听器、拦截器的方法
  • spring boot如何使用spring AOP实现拦截器
  • spring boot如何添加拦截器
  • SpringBoot拦截器实现对404和500等错误的拦截
  • spring boot实现过滤器和拦截器demo
  • springboot实现拦截器之验证登录示例
  • springboot注册拦截器所遇到的问题

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读