在Spring Boot中设置了HandlerInterceptor,发现对于js、css等文件都没有起作用。定义一个HandlerInterceptor
将HandlerInterceptor匹配到所有路径
这时虽然PathPatterns设置了“/**”,但是发现FooInterceptor对静态资源没有起作用。这时看看addInterceptors方法上的注释。
Add Spring MVC lifecycle interceptors for pre- and post-processing of controller method invocations. Interceptors can be registered to apply to all requests or be limited to a subset of URL patterns.Notethat interceptors registered here only apply to controllers and not to resource handler requests. To intercept requests for static resources either declare a MappedInterceptor bean or switch to advanced configuration mode by extending WebMvcConfigurationSupport and then override resourceHandlerMapping.
说明它只对controller起作用,如果想对静态资源起作用,简单的方法是添加一个MappedInterceptor bean。
领取专属 10元无门槛券
私享最新 技术干货