详解SpringBoot开发使用@ImportResource注解影响拦截器
问题描述
今天在给SpringBoot项目配置拦截器的时候发现怎么都进不到拦截器的方法里面,在搜索引擎上看了无数篇关于配置拦截器的文章都没有找到解决方案。
就在我准备放弃的时候,在 CSDN 上发现了一篇文章,说的是SpringBoot 用了@ImportResource 配置的拦截器就不起作用了。于是我就赶紧到Application启动类看了一眼,果然项目中使用了@ImportResource 注解用于配置系统的参数。
代码如下:
启动类配置
package com.xx.xxx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@EnableDiscoveryClient
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,
ThymeleafAutoConfiguration.class})
@SpringBootApplication
// 注意这里 !!!!
@ImportResource(locations={"classpath:config/application-*.xml"})
@EnableHystrix
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
拦截器配置
package com.xx.xxx.config;
import com.example.springbootdemo.Interceptor.LoginInterceptor;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
/**
* 拦截器(用户登录验证)
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用户排除拦截
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/user","/login");
super.addInterceptors(registry);
}
}
拦截器实现
package com.xx.xxx.interceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginInterceptor implements HandlerInterceptor {
private final static Logger LOGGER = LoggerFactory.getLogger(LoginInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
LOGGER.info("******进来了******");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
具体为什么使用@ImportResource注解会影响拦截器的配置,如果有机会研究一下源码或许能够找到答案。
PS : SpringBoot 版本 1.5.2
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Java中的JPA实体关系:JPA一对一,一对多(多对一),多对多
Java Persistence API(JPA)是Java平台上的一个对象关系映射(ORM)规范,用于简化数据库操作,其中实体关系的映射是核心内容之一,本文将深入浅出地探讨JPA中的三种基本实体关系类型:一对一、一对多、多对多,揭示常见问题、易错点及其避免策略,希望能帮助大家2024-06-06
Spring Boot整合阿里开源中间件Canal实现数据增量同步
这篇文章主要为大家介绍了Spring Boot整合阿里开源中间件Canal实现数据增量同步示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-06-06
Java中的List接口实现类LinkList和ArrayList详解
这篇文章主要介绍了Java中的List接口实现类LinkList和ArrayList详解,List接口继承自Collection接口,是单列集合的一个重要分支,实现了List接口的对象称为List集合,在List集合中允许出现重复的元素,所有的元素是以一种线性方式进行存储的,需要的朋友可以参考下2024-01-01
在SpringBoot 中从application.yml中获取自定义常量方式
这篇文章主要介绍了在SpringBoot 中从application.yml中获取自定义常量方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-04-04


最新评论