SpringBoot2.X中的靜態資源訪問失效


  今天開始使用SpringBoot寫項目,於是先讓其能夠訪問靜態資源,但是配置半天也不能訪問我的helloWorld頁面,原來,在SpringBoot2.x中,一下靜態資源路徑不生效了。

  classpath:/META/resources/,classpath:/resources/,classpath:/static/,classpath:/public/)不生效。

  springboot2.x以后,支持jdk1.8,運用了jdk1.8的一些特性。jdk1.8支持在接口中添加default方法,而此方法具有具體的方法實現。靜態資源和攔截器的處理,不再繼承“WebMvcConfigurerAdapter”方法。而是直接實現“WebMvcConfigurer”接口。通過重寫接口中的default方法,來增加額外的配置。

  要想能夠訪問靜態資源,請配置WebMvcConfigurer

 1 package io.guangsoft.web.config;  2 
 3 import io.guangsoft.common.interceptor.EncodingInterceptor;  4 import io.guangsoft.common.security.shiro.interceptor.PermissionInterceptorAdapter;  5 import io.guangsoft.common.utils.SpringContextHolder;  6 import io.guangsoft.web.interceptor.WebInterceptor;  7 import org.springframework.context.annotation.Bean;  8 import org.springframework.context.annotation.Configuration;  9 import org.springframework.core.env.Environment; 10 import org.springframework.web.servlet.HandlerInterceptor; 11 import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 12 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 13 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 14 
15 @Configuration 16 public class InterceptorConfig implements WebMvcConfigurer { 17 
18     /**
19  * 編碼攔截器 20      */
21  @Bean 22     public HandlerInterceptor encodingInterceptor() { 23         EncodingInterceptor encodingInterceptor = new EncodingInterceptor(); 24         return encodingInterceptor; 25  } 26 
27     /**
28  * 安全驗證攔截器 29  * 30  * @return
31      */
32  @Bean 33     public PermissionInterceptorAdapter permissionInterceptorAdapter() { 34         PermissionInterceptorAdapter permissionInterceptorAdapter = new PermissionInterceptorAdapter(); 35         return permissionInterceptorAdapter; 36  } 37 
38     /**
39  * 靜態資源攔截器 40      */
41  @Bean 42     public WebInterceptor webInterceptor() { 43         WebInterceptor webInterceptor = new WebInterceptor(); 44         return webInterceptor; 45  } 46 
47  @Override 48     public void addInterceptors(InterceptorRegistry registry) { 49         //編碼攔截器
50         registry.addInterceptor(encodingInterceptor()).addPathPatterns("/**").excludePathPatterns("/upload/**", "/static/**"); 51         //安全驗證攔截器
52         registry.addInterceptor(permissionInterceptorAdapter()).addPathPatterns("/**").excludePathPatterns("/upload/**", "/static/**"); 53         //web攔截器
54         registry.addInterceptor(webInterceptor()).addPathPatterns("/**").excludePathPatterns("/upload/**", "/static/**"); 55  } 56 
57     /**
58  * 添加靜態資源文件,外部可以直接訪問地址 59      */
60  @Override 61     public void addResourceHandlers(ResourceHandlerRegistry registry) { 62         //第一個方法設置訪問路徑前綴,第二個方法設置資源路徑
63         registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); 64         //添加對上傳文件的直接訪問
65         Environment env = SpringContextHolder.getBean(Environment.class); 66         String uploadFilePath = env.getProperty("upload-file-path"); 67         registry.addResourceHandler("/upload/**").addResourceLocations("file:" + uploadFilePath); 68  } 69 
70 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM