为什么spring boot工程添加了拦截器之后,静态资源static目录不会自动映射url?

陈祖煌 发布于 2018/11/13 22:59
阅读 751
收藏 1

静态资源位于src/main/resources/static目录下面,主要存放JS、CSS、模板文件。未添加拦截器之前,访问页面时可以正常通过http://localhost/js/XXX.js这样的方式加载js文件。但是按照如下代码的方式添加拦截器后(拦截路径“/personnal/**”),不能通过http://localhost/js/XXX.js这样的方式加载js文件。而控制台显示No mapping for GET /js/XXX.js。web容器用的是jetty

@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {
    @Autowired
    private PowerInterceptor powerInterceptor;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        super.addViewControllers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        registry.addInterceptor(powerInterceptor).addPathPatterns("/personnal/**");
    }
}

 

未添加拦截器时的启动日志:

22:54:42.491|StartupInfoLogger->logStarting|INFO | Starting MyApplication on DESKTOP-CFFRJTJ with PID 20996 (started by XXX) [ main ](StartupInfoLogger.java:50)
22:54:42.511|ication->logStartupProfileInfo|INFO | No active profile set, falling back to default profiles: default [ main ](SpringApplication.java:675)
22:54:46.618|etty.util.log.Log->initialized|INFO | Logging initialized @8403ms to org.eclipse.jetty.util.log.Slf4jLog [ main ](Log.java:193)
22:54:47.111|WebServerFactory->getWebServer|INFO | Server initialized with port: 80 [ main ](JettyServletWebServerFactory.java:143)
22:54:47.117|e.jetty.server.Server->doStart|INFO | jetty-9.4.12.v20180830; built: 2018-08-30T13:59:14.071Z; git: 27208684755d94a92186989f695db2d7b21ebc51; jvm 1.8.0_101-b13 [ main ](Server.java:371)
22:54:47.336|faultSessionIdManager->doStart|INFO | DefaultSessionIdManager workerName=node0 [ main ](DefaultSessionIdManager.java:365)
22:54:47.337|faultSessionIdManager->doStart|INFO | No SessionScavenger set, using defaults [ main ](DefaultSessionIdManager.java:370)
22:54:47.339|n.HouseKeeper->startScavenging|INFO | node0 Scavenging every 660000ms [ main ](HouseKeeper.java:149)
22:54:47.347|.h.ContextHandler$Context->log|INFO | Initializing Spring embedded WebApplicationContext [ main ](ContextHandler.java:2341)
22:54:47.348|->prepareWebApplicationContext|INFO | Root WebApplicationContext: initialization completed in 4699 ms [ main ](ServletWebServerApplicationContext.java:285)
22:54:47.793|istrationBean->addRegistration|INFO | Servlet statViewServlet mapped to [/druid/*] [ main ](ServletRegistrationBean.java:186)
22:54:47.818|istrationBean->addRegistration|INFO | Servlet dispatcherServlet mapped to [/] [ main ](ServletRegistrationBean.java:186)
22:54:47.821|terRegistrationBean->configure|INFO | Mapping filter: 'characterEncodingFilter' to: [/*] [ main ](AbstractFilterRegistrationBean.java:248)
22:54:47.822|terRegistrationBean->configure|INFO | Mapping filter: 'hiddenHttpMethodFilter' to: [/*] [ main ](AbstractFilterRegistrationBean.java:248)
22:54:47.823|terRegistrationBean->configure|INFO | Mapping filter: 'formContentFilter' to: [/*] [ main ](AbstractFilterRegistrationBean.java:248)
22:54:47.824|terRegistrationBean->configure|INFO | Mapping filter: 'requestContextFilter' to: [/*] [ main ](AbstractFilterRegistrationBean.java:248)
22:54:47.825|terRegistrationBean->configure|INFO | Mapping filter: 'webStatFilter' to urls: [/*] [ main ](AbstractFilterRegistrationBean.java:261)
22:54:47.834|onListener->contextInitialized|INFO | com.blog.listener.BlogApplicationListener实例化 [ main ](BlogApplicationListener.java:32)
22:54:47.839|andler.ContextHandler->doStart|INFO | Started o.s.b.w.e.j.JettyEmbeddedWebAppContext@51c8f62c{application,/,[file:///C:/Users/XXXX/AppData/Local/Temp/jetty-docbase.3306116515284146435.80/],AVAILABLE} [ main ](ContextHandler.java:850)
22:54:47.841|e.jetty.server.Server->doStart|INFO | Started @9667ms [ main ](Server.java:408)
22:54:47.912|nfigurationSupport->initialize|INFO | Initializing ExecutorService 'taskExecutor' [ main ](ExecutorConfigurationSupport.java:171)
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
log4j:WARN No appenders could be found for logger (druid.sql.Connection).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
22:54:49.327|uid.pool.DruidDataSource->init|INFO | {dataSource-1} inited [ main ](DruidDataSource.java:930)
22:54:51.852|comePageHandlerMapping-><init>|INFO | Adding welcome page template: index [ main ](WelcomePageHandlerMapping.java:61)
22:54:51.992|nfigurationSupport->initialize|INFO | Initializing ExecutorService 'taskScheduler' [ main ](ExecutorConfigurationSupport.java:171)
22:54:52.208|.h.ContextHandler$Context->log|INFO | Initializing Spring DispatcherServlet 'dispatcherServlet' [ main ](ContextHandler.java:2341)
22:54:52.208|meworkServlet->initServletBean|INFO | Initializing Servlet 'dispatcherServlet' [ main ](FrameworkServlet.java:521)
22:54:52.224|meworkServlet->initServletBean|INFO | Completed initialization in 15 ms [ main ](FrameworkServlet.java:543)
22:54:52.307|ver.AbstractConnector->doStart|INFO | Started ServerConnector@78fc0889{HTTP/1.1,[http/1.1]}{0.0.0.0:80} [ main ](AbstractConnector.java:292)
22:54:52.310|.e.jetty.JettyWebServer->start|INFO | Jetty started on port(s) 80 (http/1.1) with context path '/' [ main ](JettyWebServer.java:157)
22:54:52.317|.StartupInfoLogger->logStarted|INFO | Started BlogApplication in 12.382 seconds (JVM running for 14.144) [ main ](StartupInfoLogger.java:59)

 

添加拦截器后的启动日志:

22:44:15.964|StartupInfoLogger->logStarting|INFO | Starting MyApplication on DESKTOP-CFFRJTJ with PID 24020 (started by XXXX) [ main ](StartupInfoLogger.java:50)
22:44:16.017|ication->logStartupProfileInfo|INFO | No active profile set, falling back to default profiles: default [ main ](SpringApplication.java:675)
22:44:18.837|etty.util.log.Log->initialized|INFO | Logging initialized @5087ms to org.eclipse.jetty.util.log.Slf4jLog [ main ](Log.java:193)
22:44:19.073|WebServerFactory->getWebServer|INFO | Server initialized with port: 80 [ main ](JettyServletWebServerFactory.java:143)
22:44:19.079|e.jetty.server.Server->doStart|INFO | jetty-9.4.12.v20180830; built: 2018-08-30T13:59:14.071Z; git: 27208684755d94a92186989f695db2d7b21ebc51; jvm 1.8.0_101-b13 [ main ](Server.java:371)
22:44:19.286|faultSessionIdManager->doStart|INFO | DefaultSessionIdManager workerName=node0 [ main ](DefaultSessionIdManager.java:365)
22:44:19.288|faultSessionIdManager->doStart|INFO | No SessionScavenger set, using defaults [ main ](DefaultSessionIdManager.java:370)
22:44:19.292|n.HouseKeeper->startScavenging|INFO | node0 Scavenging every 660000ms [ main ](HouseKeeper.java:149)
22:44:19.302|.h.ContextHandler$Context->log|INFO | Initializing Spring embedded WebApplicationContext [ main ](ContextHandler.java:2341)
22:44:19.302|->prepareWebApplicationContext|INFO | Root WebApplicationContext: initialization completed in 3016 ms [ main ](ServletWebServerApplicationContext.java:285)
22:44:19.384|istrationBean->addRegistration|INFO | Servlet statViewServlet mapped to [/druid/*] [ main ](ServletRegistrationBean.java:186)
22:44:19.389|istrationBean->addRegistration|INFO | Servlet dispatcherServlet mapped to [/] [ main ](ServletRegistrationBean.java:186)
22:44:19.391|terRegistrationBean->configure|INFO | Mapping filter: 'characterEncodingFilter' to: [/*] [ main ](AbstractFilterRegistrationBean.java:248)
22:44:19.393|terRegistrationBean->configure|INFO | Mapping filter: 'webStatFilter' to urls: [/*] [ main ](AbstractFilterRegistrationBean.java:261)
22:44:19.415|andler.ContextHandler->doStart|INFO | Started o.s.b.w.e.j.JettyEmbeddedWebAppContext@4c361f63{application,/,[file:///C:/Users/XXXX/AppData/Local/Temp/jetty-docbase.3528779817640078301.80/],AVAILABLE} [ main ](ContextHandler.java:850)
22:44:19.416|e.jetty.server.Server->doStart|INFO | Started @5668ms [ main ](Server.java:408)
22:44:19.495|nfigurationSupport->initialize|INFO | Initializing ExecutorService 'taskExecutor' [ main ](ExecutorConfigurationSupport.java:171)
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
log4j:WARN No appenders could be found for logger (druid.sql.Connection).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
22:44:20.223|uid.pool.DruidDataSource->init|INFO | {dataSource-1} inited [ main ](DruidDataSource.java:930)
22:44:21.155|nfigurationSupport->initialize|INFO | Initializing ExecutorService 'taskScheduler' [ main ](ExecutorConfigurationSupport.java:171)
22:44:21.441|.h.ContextHandler$Context->log|INFO | Initializing Spring DispatcherServlet 'dispatcherServlet' [ main ](ContextHandler.java:2341)
22:44:21.442|meworkServlet->initServletBean|INFO | Initializing Servlet 'dispatcherServlet' [ main ](FrameworkServlet.java:521)
22:44:21.466|meworkServlet->initServletBean|INFO | Completed initialization in 24 ms [ main ](FrameworkServlet.java:543)
22:44:21.533|ver.AbstractConnector->doStart|INFO | Started ServerConnector@57151b3a{HTTP/1.1,[http/1.1]}{0.0.0.0:80} [ main ](AbstractConnector.java:292)
22:44:21.535|.e.jetty.JettyWebServer->start|INFO | Jetty started on port(s) 80 (http/1.1) with context path '/' [ main ](JettyWebServer.java:157)
22:44:21.540|.StartupInfoLogger->logStarted|INFO | Started BlogApplication in 6.813 seconds (JVM running for 7.791) [ main ](StartupInfoLogger.java:59)

 

发现启动日志中,添加了拦截器后的启动时少了下面三个过滤器:

Mapping filter: 'hiddenHttpMethodFilter' to: [/*]

Mapping filter: 'formContentFilter' to: [/*]

Mapping filter: 'requestContextFilter' to: [/*]

 

 

加载中
0
Simmy
Simmy

你把自动配置的覆盖了。

先好好看下文档:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-auto-configuration

If you want to keep Spring Boot MVC features and you want to add additional MVC configuration (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMappingRequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, you can declare a WebMvcRegistrationsAdapter instance to provide such components.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.

 

解决方法:

 

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    private final TestInterceptor testInterceptor;

    public InterceptorConfig(TestInterceptor testInterceptor) {
        this.testInterceptor = testInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(testInterceptor).addPathPatterns("/test/**");
    }
}

 

0
Anur
Anur
 @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(powerInterceptor).addPathPatterns("/personnal/**");
        super.addInterceptors(registry);
    }

我怀疑这里,你把原来的拦截器给覆盖了(我瞎说的,不过你可以试试)

 

0
弎笑
弎笑

为什么不用shi'ro

0
ge洋
ge洋
@Configuration
public class WebConfig implements WebMvcConfigurer {
    
    @Autowired
    private JwtInterceptor jwtInterceptor;
    
   /**
    * APP接口拦截器
    * */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor).addPathPatterns("/app/**");
    }
  

}

 

返回顶部
顶部