若是是在舊版,須要訪問下圖靜態資源,
html
地址應該爲: localhost:8080/admin/js/base.js
緣由在於:META-INF/resources / resources / static / public 都是spring boot 認爲靜態資源應該放置的位置,會自動去尋找靜態資源web
BaseInterceptor
BaseInterceptor
應實現 HandlerInterceptor
在其 preHandle
中 須要重寫,自定義攔截配置,大概以下圖:
spring
做用:攔截沒有登陸,並試圖知己訪問後臺的地址的操做。springboot
WebMvcConfigurer
WebMvcConfigurerAdapter
類,但因爲2.0後,WebMvcConfigurer
接口中定義了不少default
方法(基於jdk1.8+ )WebMvcConfigurer
中配置以下:mvc
注意:須要在重寫的第二個方法中,添加靜態資源路徑,這是可否被識別的關鍵ide
@Component public class WebMvcConfig implements WebMvcConfigurer { @Resource private BaseInterceptor baseInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(baseInterceptor) //須要配置2:----------- 告知攔截器:/static/admin/** 與 /static/user/** 不須要攔截 (配置的是 路徑) .excludePathPatterns("/static/admin/**", "/static/user/**"); } /** * 添加靜態資源文件,外部能夠直接訪問地址 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //其餘靜態資源,與本文關係不大 registry.addResourceHandler("/upload/**").addResourceLocations("file:"+ TaleUtils.getUplodFilePath()+"upload/"); //須要配置1:----------- 須要告知系統,這是要被當成靜態文件的! //第一個方法設置訪問路徑前綴,第二個方法設置資源路徑 registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); } @Override public void addViewControllers(ViewControllerRegistry registry) { //registry.addViewController("/error/404").setViewName("/admin/page_error/error_404.html"); } }
http://localhost:9090/static/admin/js/base.js
,便可正常訪問到。/admin/js/base.js
而應該在前方加上addResourceHandlers
中配置的名字https://my.oschina.net/dengfuwei/blog/1795346spa
WebMvcConfig
的addInterceptors
方法中,添加排除路徑;WebMvcConfig
中的 addInterceptors
排除路徑,原來雖然能訪問到靜態資源,可是仍是被攔截器中的 preHandle
攔截!WebMvcConfig
類中內容已更改~