SpringBoot 2.x.x會攔截靜態資源問題


 

之前在看尚硅谷視頻時,可謂是按照這老師的說法,一步一步按部就班,於是采坑了,在SpringBoot 2.x.x之前是不會對靜態資源攔截的,但是現在已經普通使用2.x.x這個版本會對靜態資源進行攔截。看如下代碼。

    //WebMvcConfigurerAdapter中注冊攔截器
 @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**"). excludePathPatterns("/index.html","/","/user/login","/webjars/**","/static/**"); }

 

     這是我在遇到問題網上查看的寫法,但是就是行不通。其實和static沒有關系,因為static這種是默認類路徑下的資源,在url上也不會***/static/js訪問,如查看源代碼,所以靜態資源js、css是直接被訪問的。

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" };

  以下是我的寫法:

  

 //WebMvcConfigurerAdapter中注冊攔截器
 @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**"). excludePathPatterns("/index.html","/","/user/login","/webjars/**","/css/**","/js/**","/img/**");
    // index.html、/、/user/login分別是對訪問首頁和 表單提交不攔截
    //
/webjars/**是對webjars依賴進來的文件不攔截
    // /css/**,/js/**,/img/** 是對static下靜態資源不攔截,注意在SprngBoot 2.x.x中是要這樣指定的,2.x.x之前不需要
    
    //-----------------建議將靜態資源放在/static/下的XX文件夾下攔截規范可以簡化成 “/XX/**”--------------

}

 


免責聲明!

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



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