当前位置: 首页 » 产品 » 母婴用品 » 正文

SpringBoot拦截器的使用技巧

放大字体  缩小字体 发布日期: 2024-09-25 12:33   来源:http://www.baidu.com/  作者:无忧资讯  浏览次数:32
核心提示:总结一下SpringBoot下拦截器的使用,步骤很简单:1.自定义自己的拦截类,拦截类需要继承HandlerInterceptor接口并实现这个接口的

总结一下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应用即可。

 
 
[ 产品搜索 ]  [ 加入收藏 ]  [ 告诉好友 ]  [ 打印本文 ]  [ 违规举报 ]  [ 关闭窗口 ]

 

 
推荐图文
推荐产品
点击排行
    行业协会  备案信息  可信网站