今天想测试下线上代码,能否正常的执行未登录的拦截。所以把拦截器的代码给开放出来,但是没想到线上代码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. [NOIP2006] 提高组 洛谷P1063 能量项链

    题目描述 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对于相邻的两颗珠子,前一颗珠子的尾标记一定 ...

  2. SOJ 2800_三角形

    真的是O不是0[看了discuss才发现.....一个大写的蠢 [题意]多个黑白三角形组成的倒三角,求白三角形组成的最大倒三角的面积 [分析]由于问的是倒三角个数,所以只需看与行数奇偶性相同的白色倒三 ...

  3. MySQL JDBC URL参数(转)

    MySQL的 JDBC URL格式: jdbc:mysql://[host][,failoverhost...][:port]/[database] » [?propertyName1][=prope ...

  4. cache and database

    This article referenced from http://coolshell.cn/articles/17416.html We all know that high concurren ...

  5. [转]chrome 的devtools 中setting 开启workspace , 也有点用处。不是很大

    转载的,原文: http://wiki.jikexueyuan.com/project/chrome-devtools/saving-changes-with-workspaces.html ---- ...

  6. MySQL 高可用架构在业务层面的分析研究

    )读多写少 虚线表示跨机房部署,比方电子商务系统.一个Master既有读也有些写.对读数据一致性须要比較重要的.读要放在Master上面. M(R)仅仅是一个备库.仅仅有M(WR)挂了之后,才会切换到 ...

  7. VS打包部署图文具体步骤及程序防卸载的制作(password验证卸载)

    1.  在vs2010 选择"新建项目->"其它项目类型"->" Visual StudioInstallerà "安装项目": ...

  8. HDU1215--七夕节

    找出小于N的全部因子的和,N比較大,非常明显要打表来做,不然肯定会超时 方法就是枚举范围内每一个整数.然后再枚举范围内这个整数的全部的倍数,加上这个数 由于这个整数的倍数中一定含有这个整数因子,这样速 ...

  9. 找了两个node+vue-cli+mogodb的小项目学学

    实现的博客系统,自己抽时间搭建看看 https://www.cnblogs.com/yesyes/p/7188331.html https://github.com/chaohangz/vueBlog

  10. OpenFileDiag 使用

    MSDN模版 Stream myStream =null; OpenFileDialog openFileDialog1 =newOpenFileDialog(); openFileDialog1.I ...