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版本拦截器 的实现的更多相关文章

  1. SpringBoot2.0针对请求参数@RequestBody验证统一拦截

    title: "SpringBoot2.0针对请求参数@RequestBody验证的统一拦截"categories: SpringBoot2.0 Shirotags: Spring ...

  2. SpringBoot2.0 基础案例(05):多个拦截器配置和使用场景

    一.拦截器简介 1.拦截器定义 拦截器,请求的接口被访问之前,进行拦截然后在之前或之后加入某些操作.拦截是AOP的一种实现策略. 拦截器主要用来按照指定规则拒绝请求. 2.拦截器中应用 Token令牌 ...

  3. springboot2.0.4对接redis3.2.12版本哨兵模式

    redis 哨兵模式的创建 1. 下载redis3.2.12版本.https://codeload.github.com/antirez/redis/zip/3.2.12 2.  解压后放到/usr/ ...

  4. SpringBoot2.0 整合 QuartJob ,实现定时器实时管理

    一.QuartJob简介 1.一句话描述 Quartz是一个完全由java编写的开源作业调度框架,形式简易,功能强大. 2.核心API (1).Scheduler 代表一个 Quartz 的独立运行容 ...

  5. springboot2.0+ 使用拦截器导致静态资源被拦截

    在spring1.0+的版本中,配置拦截器后是不会拦截静态资源的.其配置如下: @Configuration public class WebMvcConfig extends WebMvcConfi ...

  6. 升级项目版本:SpringBoot1.5.x到SpringBoot2.0.x

    1.升级版本的选择 首先去spring的官网看一下最新的版本与版本之间的依赖

  7. Springboot2.0实现URL拦截

    1.创建一个登陆拦截器SecurityInterceptor,它继承HandlerInterceptorAdapter类 package com.cn.commodity.config; import ...

  8. 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 ...

  9. 零基础快速入门SpringBoot2.0教程 (二)

    一.SpringBoot2.x使用Dev-tool热部署 简介:介绍什么是热部署,使用springboot结合dev-tool工具,快速加载启动应用 官方地址:https://docs.spring. ...

随机推荐

  1. shell查词典

    curl http://cn.bing.com/dict/search?q=spawn -s | sed -e '{s/<\/span>/&\n/g}' | sed -n '{/& ...

  2. 【python】 字符串转小写(含汉字等时仍work)

    def mylower(str): outstr = ""; strlen = len(str); idx = 0; while idx < strlen: if ord(s ...

  3. CommonJS规范 by ranyifeng

    1,概述 CommonJS是服务器端模块的规范,Node.js采用了这个规范. 根据CommonJS规范,一个单独的文件就是一个模块.加载模块使用require方法,该方法读取一个文件并执行,最后返回 ...

  4. Spring Boot 如何防止重复提交?

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 在传统的web项目中,防止重复提交,通常做法是:后端生成一个唯一的提交令牌(uuid),并存储在服务端.页面提交请求携带这个 ...

  5. 【知识强化】第四章 网络层 4.7 IP组播

    这节课我们来学习一下IP组播. 首先我们来看这样一个问题,IP数据报在网络当中传输的时候,有几种传输方式呢?三种,分别是单播.广播和组播(多播).这个组播呢也叫做多播,它们俩是一个意思.那这个组播是由 ...

  6. 【知识强化】第六章 查找 6.3 B树和B+树

    本节课我们来学习本章的第一个难点,就是B树.那么B树它其实是一种数据结构,我们设计出这种数据结构就是为了提高我们的查找效率的,提高我们在磁盘上的查找效率.那么什么是B树呢?了解B树之前,我们先来回忆一 ...

  7. Linux 核心编译与管理

    一般情况下,不需要重新编译核心,除非以下特有的用途 [root@localhost ~]# wget ftp://ftp.twaren.net/pub/Unix/Kernel/linux/kernel ...

  8. go语言从例子开始之Example15.闭包

    Go 支持通过 闭包来使用 匿名函数.匿名函数在你想定义一个不需要命名的内联函数时是很实用的. 闭包简单理解,函数反回值是一个函数 Example: package main import " ...

  9. 转:KVM 虚拟机的克隆

    KVM 虚拟机的克隆 首先把需要克隆的源虚拟机先关闭,然后使用以下命令来进行克隆,注意我这里使用的是相对路径.   virsh shutdown VM02 virt-clone -o VM02 -n ...

  10. 运用pool进程池启动大量子进程

    # Pool进程池类 from multiprocessing import Pool import os import time import random def run(index): prin ...