spring boot 拦截器的使用 SpringBoot拦截器的使用小结

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

spring boot 拦截器的使用 SpringBoot拦截器的使用小结

说话的方式简单点丶   2021-03-24 我要评论
想了解SpringBoot拦截器的使用小结的相关内容吗,说话的方式简单点丶在本文为您仔细讲解spring boot 拦截器的使用的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:spring,boot,拦截器,spring,boot,拦截,下面大家一起来学习吧。

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

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

@Override
  public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
    //方法调用前执行
    return true;//返回为false,拦截器拦截的方法不会调用
  }
  @Override
  public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
  //方法执行结束后执行
  }
  @Override
  public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, 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拦截器的使用小结,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们