为什么spring boot工程添加了拦截器之后,静态资源static目录不会自动映射url?
< 返回列表时间: 2018-11-13来源:开源中国
HDC调试需求开发(15万预算),能者速来!>>>
静态资源位于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: [/*]

热门排行