<强>问题描述强>
今天在给SpringBoot项目配置拦截器的时候发现怎么都进不到拦截器的方法里面,在搜索引擎上看了无数篇关于配置拦截器的文章都没有找到解决方案。
就在我准备放弃的时候,在CSDN上发现了一篇文章,说的是SpringBoot用了@ImportResource配置的拦截器就不起作用了。于是我就赶紧到<代码> 代码>应用启动类看了一眼,果然项目中使用了<代码> @ImportResource 代码>注解用于配置系统的参数。
代码如下:
<>强启动类配置
强>
包com.xx.xxx; 进口org.springframework.boot.SpringApplication; 进口org.springframework.boot.autoconfigure.EnableAutoConfiguration; 进口org.springframework.boot.autoconfigure.SpringBootApplication; 进口org.springframework.boot.builder.SpringApplicationBuilder; 进口org.springframework.boot.web.servlet.ServletComponentScan; 进口org.springframework.boot.web.support.SpringBootServletInitializer; 进口org.springframework.context.annotation.Configuration; 进口org.springframework.context.annotation.ImportResource; @EnableDiscoveryClient @EnableAutoConfiguration(排除={DataSourceAutoConfiguration.class, ThymeleafAutoConfiguration.class}) @SpringBootApplication//注意这里! ! ! ! @ImportResource(位置={"类路径:配置/应用程序- * . xml”}) @EnableHystrix 公共类的应用程序扩展SpringBootServletInitializer { 公共静态void main (String [] args) { SpringApplication.run (Application.class, args); } }
<>强拦截器配置
强>
包com.xx.xxx.config; 进口com.example.springbootdemo.Interceptor.LoginInterceptor; 进口org.springframework.boot.autoconfigure.EnableAutoConfiguration; 进口org.springframework.cache.annotation.EnableCaching; 进口org.springframework.context.annotation.Bean; 进口org.springframework.context.annotation.ComponentScan; 进口org.springframework.context.annotation.Configuration; 进口org.springframework.context.annotation.ImportResource; 进口org.springframework.web.servlet.config.annotation.InterceptorRegistry; 进口org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 进口org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @ configuration 公开课WebMvcConfig延伸WebMvcConfigurerAdapter {/* * *拦截器(用户登录验证) * @param注册表 */@Override 公共空间addInterceptors (InterceptorRegistry注册表){//addPathPatterns用于添加拦截规则//excludePathPatterns用户排除拦截 注册表。addInterceptor(新LoginInterceptor ()) .addPathPatterns .excludePathPatterns(“/* *”)(“/用户”、“/登录”); super.addInterceptors(注册表); } }
<>强拦截器实现强>
包com.xx.xxx.interceptor; 进口org.springframework.stereotype.Component; 进口org.springframework.web.servlet.HandlerInterceptor; 进口org.springframework.web.servlet.ModelAndView; 进口javax.servlet.annotation.WebServlet; 进口javax.servlet.http.HttpServletRequest; 进口javax.servlet.http.HttpServletResponse; 公共类LoginInterceptor实现HandlerInterceptor { 私人最终静态日志记录器=LoggerFactory.getLogger (LoginInterceptor.class); @Override 公共布尔preHandle (HttpServletRequest请求,HttpServletResponse响应对象处理程序){抛出异常 LOGGER.info(“* * * * * *进来了* * * * * *”); 返回true; } @Override 公共空白postHandle (HttpServletRequest请求,HttpServletResponse响应对象处理程序,ModelAndView ModelAndView){抛出异常 } @Override 公共空间afterCompletion (HttpServletRequest请求,HttpServletResponse响应、对象处理程序异常交货){抛出异常 } } >之前具体为什么使用@ImportResource注解会影响拦截器的配置,如果有机会研究一下源码或许能够找到答案。
PS: SpringBoot版本1.5.2
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
详解SpringBoot开发使用@ImportResource注解影响拦截器