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. Html5 学习笔记 【PC固定布局】 实战3 热门旅游展示区

    最终效果图: html 代码: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta char ...

  2. 用select实际非阻塞I/O

    非阻塞read/write 函数返回0表示可读或可写, -1表示select失败或超时 select返回0表示超时,-1表示读取失败,1表示可读或可写 int read_timeout(int fd, ...

  3. Vue2.0---vuex初理解

    先来一张vuex的帅照 第一眼看到这张图片我内心是万匹草泥马飞过. 简单理解:  vuex:一个可以全局被使用的状态管理的“仓库”:state.js中定义初始状态,通过action去触发mutatio ...

  4. Java并发AtomicIntegerArray类

    java.util.concurrent.atomic.AtomicIntegerArray类提供了可以以原子方式读取和写入的底层int数组的操作,还包含高级原子操作. AtomicIntegerAr ...

  5. W3C、MDN及html常用标签介绍

    W3C 万维网(World Wide Web)是作为欧洲核子研究组织的一个项目发展起来的,在那里 Tim Berners-Lee 开发出万维网的雏形.Tim Berners-Lee- 万维网的发明人和 ...

  6. springCloud的使用06-----分布式配置

    1 分布式配置中心的搭建 1.1 在git仓库中创建配置文件 1.2 创建springboot项目引入相应jar依赖 <project xmlns="http://maven.apac ...

  7. Codeforces 492E Vanya and Field

    E. Vanya and Field time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. vs 删除代码中空行

    原文:vs 删除代码中空行 ^\s*\n 正则表达式

  9. 2019HDU多校训练第二场 Longest Subarray

    题意:给你一个串,问满足以下条件的子串中最长的是多长:对于每个数字,要么在这个子串没出现过,要么出现次数超过k次. 思路:对于这类问题,常常转化为数据结构的询问问题.我们考虑枚举右端点,对于当前右端点 ...

  10. Codeforces 360C DP 计算贡献

    题意:给你一个长度为n的字符串,定义两个字符串的相关度为两个串对应的子串中第一个串字典序大于第二个串的个数.现在给你相关度,和第二个串,问满足条件的第一个串有多少个? 思路:设dp[i][j]为填了前 ...