今天想测试下线上代码,能否正常的执行未登录的拦截。所以把拦截器的代码给开放出来,但是没想到线上代码addInerceptors(InterceptorRegistry registry) 这个方法一直不被执行。

@EnableWebMvc
@Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer { @Bean
public UnLoginInterceptor unLoginInterceptor(){
return new UnLoginInterceptor();
} @Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowCredentials(true)
.allowedMethods("GET", "POST", "DELETE", "PUT", "HEAD", "OPTIONS")
.maxAge(3600);
} @Override
public void addInterceptors(InterceptorRegistry registry) {
log.info("加载拦截器");
//添加拦截器并添加拦截器的拦截路径
registry.addInterceptor(unLoginInterceptor()).addPathPatterns("/api/**")
.excludePathPatterns("/api/web/login/**")
.excludePathPatterns("/api/web/dictionary/**")
.excludePathPatterns("/api/web/perm/listAllPerm");
}
}

这里请大家注意@EnableWebMvc这个注解。问题就出在这里了。跟进这个注解会发现它有一个代理类DelegatingWebMvcConfiguration。

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}

再跟进会发现它继承了 WebMvcConfigurationSupport。

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite(); public DelegatingWebMvcConfiguration() {
}
}

请大家记住这个类WebMvcConfigurationSupport。

---------------------------------------------------------------------

其实Springboot的自动装配依靠的是WebMvcAutoConfiguration类和其类中静态内部类WebMvcAutoConfigurationAdapter以及EnableWebMvcConfiguration构成了自动装配。

那么可以很清楚的看到WebMvcAutoConfiguration类名上面有一个注解@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})。意思就是如果当前的Spring类中不包含WebMvcConfigurationSupport这个类,那么会走自动装配。否则就需要自己去装配bean。

@Configuration
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
public static final String DEFAULT_PREFIX = "";
public static final String DEFAULT_SUFFIX = "";
private static final String[] SERVLET_LOCATIONS = new String[]{"/"}; public WebMvcAutoConfiguration() {
}
}

看到这里,问题就迎刃而解了,就是这个注解@EnableWebMvc导致了我的拦截器不起作用。

那么问题来了?为什么本地的可以被拦截到,而线上的代码不能被拦截呢?其实我也不知道。我只是找到了问题的所在。但是解释不了为什么本地跟线上的不一样。我怀疑是代码打包的有问题,然后用反编译工具,比对代码。发现的确代码都是被成功打包了。所以究竟是什么原因导致线上代码拦截器不起作用?我也不知道。嘻嘻嘻!望指教。。。

Springboot拦截器线上代码失效的更多相关文章

  1. Java结合SpringBoot拦截器实现简单的登录认证模块

    Java结合SpringBoot拦截器实现简单的登录认证模块 之前在做项目时需要实现一个简单的登录认证的功能,就寻思着使用Spring Boot的拦截器来实现,在此记录一下我的整个实现过程,源码见文章 ...

  2. SpringBoot拦截器中无法注入bean的解决方法

    SpringBoot拦截器中无法注入bean的解决方法 在使用springboot的拦截器时,有时候希望在拦截器中注入bean方便使用 但是如果直接注入会发现无法注入而报空指针异常 解决方法: 在注册 ...

  3. Springboot 拦截器配置(登录拦截)

    Springboot 拦截器配置(登录拦截) 注意这里环境为springboot为2.1版本 1.编写拦截器实现类,实现接口   HandlerInterceptor, 重写里面需要的三个比较常用的方 ...

  4. springboot拦截器总结

    Springboot 拦截器总结 拦截器大体分为两类 : handlerInterceptor 和 methodInterceptor 而methodInterceptor 又有XML 配置方法 和A ...

  5. SpringBoot拦截器中Bean无法注入(转)

    问题 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于其他bean在service,controller层注入一点问题也没有,开始根本没意识到Be ...

  6. SpringBoot拦截器中service或者redis注入为空的问题

    原文:https://my.oschina.net/u/1790105/blog/1490098 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于 ...

  7. springboot + 拦截器 + 注解 实现自定义权限验证

    springboot + 拦截器 + 注解 实现自定义权限验证最近用到一种前端模板技术:jtwig,在权限控制上没有用springSecurity.因此用拦截器和注解结合实现了权限控制. 1.1 定义 ...

  8. 不停机替换线上代码? 你没听错,Arthas它能做到

    写在前边 有没有这样一种感受,自己写的代码在开发.测试环境跑的稳得一笔,可一到线上就抽风,不是缺这个就是少那个反正就是一顿报错,线上调试代码又很麻烦,让人头疼得很.阿里巴巴出了一款名叫Arthas的工 ...

  9. SpringBoot 拦截器获取http请求参数

    SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 目录 SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 获取http请求参数是一种刚需 定义拦截器获取请求 为 ...

随机推荐

  1. 【ZJOI2017 Round1练习】D7T1 graph(提答)

    题意: n<=1000 m<=10000 思路:

  2. HDU——1179 Ollivanders: Makers of Fine Wands since 382 BC.

    Ollivanders: Makers of Fine Wands since 382 BC. Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  3. 笔记:Javac编译器

    Javac编译器是把 *.java 文件转换为 *.class 文件,是一个前端编译器:对应着有一种把字节码转变为机器码的编译器,称为JIT编译器(Just In Time Compiler),比如 ...

  4. NBUT 1450 Blitzcrank

    [1450] Blitzcrank 时间限制: 1000 ms 内存限制: 65535 K 问题描写叙述 Blitzcrank is a robot. There are some pretty go ...

  5. day4-hdfs的核心工作原理\写数据流程 \读数据流程

    namenode元数据管理要点 1.什么是元数据? hdfs的目录结构及每一个文件的块信息(块的id,块的副本数量,块的存放位置<datanode>) 2.元数据由谁负责管理? namen ...

  6. hdu 1565 方格取数(1)(状态压缩dp)

    方格取数(1)                                                                 Time Limit: 10000/5000 MS (J ...

  7. Android学习笔记之Spinner下拉列表使用案例

    (1)两种方法提冲Spinner中的数据源:通过list集合,或者是通过xml文件进行配置 (2)布局代码例如以下: <RelativeLayout xmlns:android="ht ...

  8. Matplotlib作图基础

    折线图 import matplotlib.pylab as pylab import numpy as npy x=[1,2,3,4,8] y=[5,7,2,1,5] #折线图 pylab.plot ...

  9. UILongPressGestureRecognizer 运行两次的解决的方法

    近期维护之前用iOS SDK 3.2写过的3年多前的map方面的模块,在地图上长按pin,发觉一个点莫名奇异点插了两个pin. 查了一下,原来是如今的sdk要在UILongPressGestureRe ...

  10. 【Android】自己定义View

    翻译自:http://developer.android.com/training/custom-views/index.html 一)创建view类 一个设计良好的自己定义view与其它的类一样.它 ...