今天想测试下线上代码,能否正常的执行未登录的拦截。所以把拦截器的代码给开放出来,但是没想到线上代码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. HDU-2817,同余定理+快速幂取模,水过~

    A sequence of numbers                                                             Time Limit: 2000/1 ...

  2. [luoguP1439] 排列LCS问题(DP + 树状数组)

    传送门 无重复元素的LCS问题 n2 做法不说了. nlogn 做法 —— 因为LCS问题求的是公共子序列,顺序不影响答案,影响答案的只是两个串的元素是否相同,所以可以交换元素位置. 首先简化一下问题 ...

  3. hdu 1027

    #include<stdio.h> #include<algorithm> using namespace std; int a[1100]; int main() {  in ...

  4. Flask基础(3):session、flash、特殊装饰器、蓝图、路由正则匹配、上下文管理 & flask-session

    Session: Flask 默认将 session 以加密的形式放到了浏览器的 cookie 中 Flask 的 session 就是一个字典,字典有什么方法 session 就有什么方法 flas ...

  5. sql 日期问题从周转换到日期

    alter procedure p_date@year int=2005,    --年份@week int=33,    --第几周@firstday datetime =null output,  ...

  6. 【c++】【转】C++ sizeof 使用规则及陷阱分析

    http://www.cnblogs.com/chio/archive/2007/06/11/778934.html sizeof不是函数,更像一个特殊的宏,它是在编译阶段求值得.sizeof作用范围 ...

  7. Windows系统下JAVA开发环境搭建

    首先我们需要下载JDK(JAVA Development Kit),JDK是整个java开发的核心,它包含了JAVA的运行环境,JAVA工具和JAVA基础的类库. 下载地址:http://www.or ...

  8. create-react-app 引入 antd 及 解决 antd 样式无法显示的bug

    方案一: npm run eject 暴露所有内建的配置 安装组件库 yarn add antd babel-plugin-import 根目录下新建.roadhogrc文件(别忘了前面的点,这是ro ...

  9. 演练:我的第一个 WPF 桌面应用程序 https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/getting-started/walkthrough-my-first-wpf-desktop-application

    这篇文章演示如何开发简单的 Windows Presentation Foundation (WPF) 应用程序包括元素所共有的大多数 WPF 应用程序: 可扩展应用程序标记语言 (XAML) 标记. ...

  10. Android系统优化

    这些事实上就是优化rom 的一些实用小技巧. 认为非常多还是实用的. Build.prop (编辑 /system/build.prop 文件(须要root, 能够用文件管理器或者其它root exp ...