SpringBoot2.0拦截器 与 1.X版本拦截器 的实现
1.5 版本
先写个拦截器,跟xml配置方式一样,然后将拦截器加入spring容器管理 。接着创建 配置文件类 继承 WebMvcConfigurerAdapter 类,重写父类方法addInterceptors
@Component
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
//true放行 false拦截
System.out.println("拦截");
return true;
} @Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("preHandle()执行过后再执行");
System.out.println("controller执行前再执行");
} @Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("controller执行过后再执行");
}
}
拦截器类
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{ @Autowired
private MyInterceptor myInterceptor; @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor)
.addPathPatterns("/**")
.excludePathPatterns("/index","/login");
super.addInterceptors(registry);
} }
配置文件类
2.0 版本跟1.5一样 就是配置类需要的是实现WebMvcConfigurer接口不再是继承 WebMvcConfigurerAdapter 类 ,通过spring代码可以发现 WebMvcConfigurerAdapter implements WebMvcConfigurer 这种关系.
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Autowired
private MyInterceptor myInterceptor; @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(webInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/asserts/**","/receiveToken","/ssoLogout","/ssoDeleteToken");
;
} @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//addResourceLocations指的是文件放置的目录,addResoureHandler指的是对外暴露的访问路径
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
} }
配置文件类
SpringBoot2.0拦截器 与 1.X版本拦截器 的实现的更多相关文章
- SpringBoot2.0针对请求参数@RequestBody验证统一拦截
title: "SpringBoot2.0针对请求参数@RequestBody验证的统一拦截"categories: SpringBoot2.0 Shirotags: Spring ...
- SpringBoot2.0 基础案例(05):多个拦截器配置和使用场景
一.拦截器简介 1.拦截器定义 拦截器,请求的接口被访问之前,进行拦截然后在之前或之后加入某些操作.拦截是AOP的一种实现策略. 拦截器主要用来按照指定规则拒绝请求. 2.拦截器中应用 Token令牌 ...
- springboot2.0.4对接redis3.2.12版本哨兵模式
redis 哨兵模式的创建 1. 下载redis3.2.12版本.https://codeload.github.com/antirez/redis/zip/3.2.12 2. 解压后放到/usr/ ...
- SpringBoot2.0 整合 QuartJob ,实现定时器实时管理
一.QuartJob简介 1.一句话描述 Quartz是一个完全由java编写的开源作业调度框架,形式简易,功能强大. 2.核心API (1).Scheduler 代表一个 Quartz 的独立运行容 ...
- springboot2.0+ 使用拦截器导致静态资源被拦截
在spring1.0+的版本中,配置拦截器后是不会拦截静态资源的.其配置如下: @Configuration public class WebMvcConfig extends WebMvcConfi ...
- 升级项目版本:SpringBoot1.5.x到SpringBoot2.0.x
1.升级版本的选择 首先去spring的官网看一下最新的版本与版本之间的依赖
- Springboot2.0实现URL拦截
1.创建一个登陆拦截器SecurityInterceptor,它继承HandlerInterceptorAdapter类 package com.cn.commodity.config; import ...
- Spring-Boot-2.0.0-M1版本将默认的数据库连接池从tomcat jdbc pool改为了hikari
spring-configuration-metadata.json spring-boot-autoconfigure-2.0.0.M7.jar!/META-INF/spring-configura ...
- 零基础快速入门SpringBoot2.0教程 (二)
一.SpringBoot2.x使用Dev-tool热部署 简介:介绍什么是热部署,使用springboot结合dev-tool工具,快速加载启动应用 官方地址:https://docs.spring. ...
随机推荐
- Windows重置网络命令
我们在日常使用电脑的时候会碰到网络异常,网络故障,想要针对性地去解决是很困难的. 有时候可能查遍了资料,花了大量时间,怎么搞都还是搞不定.所以,这次直接给大家分享一个通过重置网络来解决所有问题的方法. ...
- FileInputStream_FileOutputStream
Writer的基本方法 //向输出流中写入一个字符数据,该字节数据为参数b的低16位 void write(int c) throws IOException //将一个字符类型的数组中的数据写入输出 ...
- JVM运行时区域详解。
我们知道的JVM内存区域有:堆和栈,这是一种泛的分法,也是按运行时区域的一种分法,堆是所有线程共享的一块区域,而栈是线程隔离的,每个线程互不共享. 线程不共享区域 每个线程的数据区域包括程序计数器.虚 ...
- luoguP2680 运输计划 题解(二分答案+树上差分)
P2680 运输计划 题目 这道题如果是看的我的树上差分来的,那么肯定一看题目就可以想到树上差分. 至于这是怎么想到的,一步一步来: 1.n有300000,不可能暴力枚举每一条边 2.因为我们要使运 ...
- python isinstance()函数和type()函数
一.type()用法 描述: python的 type 函数有两个用法,当只有一个参数的时候,返回对象的类型.当有三个参数的时候返回一个类对象. 语法: 一个参数:type(object) 三个参数: ...
- JAVA编程思想(1) - 一切都是对象
-"假设我们说还有一种不用的语言,那么我们就会发觉一个有些不同的世界" 1. 用引用操纵对象 每种编程语言都有自己的数据处理方式. 有些时候,程序猿必须时刻留意准备 ...
- K8S入门系列之必备扩展组件--> coredns(四)
摘要: 集群其他组件全部完成后我们应当部署集群 DNS 使 service 等能够正常解析,1.11版本coredns已经取代kube-dns成为集群默认dns. https://github.com ...
- 背包九讲(Orz)
P01: 01背包问题 题目 有\(N\)件物品和一个容量为\(V\)的背包.第\(i\)件物品的费用是\(c[i]\),价值是\(w[i]\).求解将哪些物品装入背包可使这些物品的费用总和不超过背包 ...
- Centos7 部署ftp
1.查看是否已经安装 vsftpd -version 2.安装vsftpd yum install -y vsftpd 3.新建FTP目录 mkdir /data/KodServer/data/Use ...
- oracle中sys,system,scott,hr用户
https://blog.csdn.net/xingfeng0501/article/details/6699390 scott 是个演示用户,是让你学习ORACLE用的 hr用户是个示例用户,是在创 ...