SpringBoot開發使用@ImportResource註解影響攔截器

問題描述

今天在給SpringBoot項目配置攔截器的時候發現怎麼都進不到攔截器的方法裏面,在搜索引擎上看了無數篇關於配置攔截器的文章都沒有找到解決方案。java

就在我準備放棄的時候,在 CSDN 上發現了一篇文章,說的是SpringBoot 用了@ImportResource 配置的攔截器就不起做用了。因而我就趕忙到Application啓動類看了一眼,果真項目中使用了@ImportResource 註解用於配置系統的參數。web

代碼以下:spring

啓動類配置

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

相關文章
相關標籤/搜索