序言:几乎所有项目都需要拦截器,所以小伙伴们必须要掌握这门技术哦,不然只会mybaits增删改查那是实习生干的活呀。

1、创建拦截器类,implements HandlerInterceptor

public class MyIncerteptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("进入拦截器"); HttpSession session = request.getSession();
Admin user = (Admin) request.getSession().getAttribute("ADMIN_ACCOUNT");
if (user == null) {
//用户为空,重新登录
response.sendRedirect(request.getContextPath() + "/login.html");
return false;
}
return true;
} @Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }
}

2、配置文件,注册拦截器 implements

@Configuration
public class InterceptorConfig implements WebMvcConfigurer { @Override
public void addInterceptors(InterceptorRegistry registry) { String[] addPathPatterns ={
"/dtb/**"
};
String[] excludePathPatterns ={
"/test/**"
};
InterceptorRegistration interceptorRegistration = registry.addInterceptor(new MyIncerteptor());
interceptorRegistration.addPathPatterns(addPathPatterns);
interceptorRegistration.excludePathPatterns(excludePathPatterns);
}
}

addPathPatterns是拦截的那些目录,excludePathPatterns不拦截的路径,上面我们可以换一种常用的写法,只不过上面看上去更明白,interceptorRegistration获取的注册对象添加拦截器,并设置拦截路径

@Configuration
public class InterceptorConfig implements WebMvcConfigurer { @Override
public void addInterceptors(InterceptorRegistry registry) { String[] addPathPatterns ={
"/dtb/**"
};
String[] excludePathPatterns ={
"/test/**"
};
registry.addInterceptor(new MyIncerteptor()).addPathPatterns(addPathPatterns).excludePathPatterns(excludePathPatterns);
}
}

在优化一下,可以写成:

@Configuration
public class InterceptorConfig implements WebMvcConfigurer { @Override
public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyIncerteptor()).addPathPatterns("/dtb/**").excludePathPatterns("/test/**");
}
}

成功!其实就两个类就可以了。

3、注意点:

springboot2.0以后:使用spring5,废弃了WebMvcConfigurerAdapter,解决方案 implements WebMvcConfigurer

springboot2.0之前,1.X版本,使用spring4,使用拦截器需要 extends WebMvcConfigurerAdapter

转载于:https://my.oschina.net/mdxlcj/blog/1858710

崛起于Springboot2.X之开发拦截器(21)的更多相关文章

  1. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-10.Springboot2.x用户登录拦截器开发实战

    笔记 10.Springboot2.x用户登录拦截器开发实战     简介:实战开发用户登录拦截器拦截器 LoginInterceptor                  1.实现接口 LoginI ...

  2. SpringBoot2.X自定义拦截器实战及新旧配置对比(核心知识)

    简介: 讲解拦截器使用,Spingboot2.x新版本配置拦截拦截器和旧版本SpringBoot配置拦截器区别讲解 1.@Configuration 继承WebMvcConfigurationAdap ...

  3. SpringBoot拦截器和 Servlet3.0自定义Filter、Listener

    官方文档译文 Spring Boot 包括对嵌入式Tomcat,Jetty和Undertow服务器的支持.大多数开发人员使用适当的“Starter”来获取完全配置的实例.默认情况下,嵌入式服务器在 p ...

  4. SpringBoot入门一:基础知识(环境搭建、注解说明、创建对象方法、注入方式、集成jsp/Thymeleaf、logback日志、全局热部署、文件上传/下载、拦截器、自动配置原理等)

    SpringBoot设计目的是用来简化Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,SpringBoot致力于在蓬勃发 ...

  5. Struts的拦截器

    Struts的拦截器 1.什么是拦截器 Struts的拦截器和Servlet过滤器类似,在执行Action的execute方法之前,Struts会首先执行Struts.xml中引用的拦截器,在执行完所 ...

  6. struts2 拦截器

    拦截器:对Action的访问.可以拦截到Action中某个方法.与过滤器不同,过滤器过滤的是请求.过滤JSP.html.但是拦截器不能拦截jsp.html的访问. Struts2 拦截器在访问某个 A ...

  7. Struts2第七篇【介绍拦截器、自定义拦截器、执行流程、应用】

    什么是拦截器 拦截器Interceptor-..拦截器是Struts的概念,它与过滤器是类似的-可以近似于看作是过滤器 为什么我们要使用拦截器 前面在介绍Struts的时候已经讲解过了,Struts为 ...

  8. Struts2【拦截器】就是这么简单

    什么是拦截器 拦截器Interceptor.....拦截器是Struts的概念,它与过滤器是类似的...可以近似于看作是过滤器 为什么我们要使用拦截器 前面在介绍Struts的时候已经讲解过了,Str ...

  9. SpringBoot(11) SpringBoot自定义拦截器

    自定义拦截器共两步:第一:注册.第二:定义拦截器. 一.注册 @Configuration 继承WebMvcConfigurationAdapter(SpringBoot2.X之前旧版本) 旧版本代码 ...

随机推荐

  1. 为应用程序池“XXX”提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误。该进程 ID 为“XXXX”。数据字段包含错误号。 改进查找流程

    原文链接:https://www.cnblogs.com/qidian10/p/6028784.html  为防止原作者删除,留作解决方法备份 ---------------------------- ...

  2. andlua,andlua发送http请求,并解析json数据

    andlua发送http请求,并解析json实例 import'cjson'import 'http'--导入cjson库url = 'https://www.baidu,com'--设置urlHtt ...

  3. LVS 集群与存储《路由转发》

                                                             LVS 集群与存储<路由转发> 集群简介 u 什么是集群 •  一组通过高 ...

  4. 部署并测试动态WSGI站点

                                                            部署并测试动态WSGI站点 5.1问题 本例要求为站点webapp0.example.c ...

  5. Mysql数据库卸载

    Mysql数据库卸载的操作流程(Windows10): 1.停止mysql的所有服务 方法一:此电脑——管理——服务中查找到所有Mysql的服务,并停止. 方法二:cmd——net stop mysq ...

  6. mpvue小程序开发

    查阅资料,看官方文档,知道mpvue是一个使用 Vue.js 开发小程序的前端框架(美团的开源项目).框架基于 Vue.js 核心,mpvue 修改了 Vue.js 的 runtime 和 compi ...

  7. spark foreachPartition foreach

    1.foreach val list = new ArrayBuffer() myRdd.foreach(record => { list += record }) 2.foreachParti ...

  8. 萌新带你开车上p站(三)

    本文作者:萌新 前情回顾: 萌新带你开车上p站(一) 萌新带你开车上p站(二) 0x08 题目给的提示是和运算符优先级有关 登录后直接看源码 mistake@pwnable:~$ ls flag mi ...

  9. B - How Many Equations Can You Find dfs

    Now give you an string which only contains 0, 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9.You are asked to add the sig ...

  10. [apue] getopt 可能重排参数

    看第21章时,介绍到了解析命令行的神器 getopt,了解了 linux 下处理通用命令行的方法. 命令行可分为参数与选项,其中不带 - 或 -- 前缀的为参数,对一个命令而言数量是固定的,多个参数之 ...