關(guān)于SpringBoot攔截器攔截靜態(tài)資源的問(wèn)題
SpringBoot攔截器攔截靜態(tài)資源
使用springboot2.0+的版本建立攔截器攔截訪問(wèn)時(shí),發(fā)現(xiàn)將靜態(tài)的css/js資源也攔截了。
此時(shí)需要在攔截器中配置放行資源。
直接上代碼
//設(shè)置攔截,釋放靜態(tài)文件 registry.addInterceptor(new LoginHandlerInterceptero()).addPathPatterns("/**") .excludePathPatterns("/user/login","/login.html","/css/**","/js/**");
放行"/css/**"資源。
有些使用放行static文件夾來(lái)放行所有的靜態(tài)資源,不過(guò)SpringBoot默認(rèn)掃描static包下的靜態(tài)資源,所以好像不管用。
下圖是一次項(xiàng)目中配置的靜態(tài)資源釋放,
對(duì)文件結(jié)構(gòu)不是很清楚,所以直接配置釋放所有文件。
springboot攔截器為什么攔截靜態(tài)資源
項(xiàng)目目錄結(jié)構(gòu)如下所示
??
首先是以繼承的方式注冊(cè)攔截器配置
@Configuration public class SpringMvcSupport_extends extends WebMvcConfigurationSupport { @Autowired private LoginInterceptor loginInterceptor; @Override protected void addInterceptors(InterceptorRegistry registry) { //配置攔截器 registry.addInterceptor(loginInterceptor). //對(duì)所有的資源進(jìn)行攔截,包括靜態(tài)資源 addPathPatterns("/**") .excludePathPatterns("/login.html","/js/**","/css/**","/lib/**"); }
此時(shí)訪問(wèn)靜態(tài)資源得到如下結(jié)果:
這種方式會(huì)攔截靜態(tài)資源我也不知道原因是什么希望后續(xù)補(bǔ)坑
但是如果以實(shí)現(xiàn)接口的形式來(lái)注冊(cè)攔截器的話就不會(huì)攔截靜態(tài)資源
@Configuration public class SpringMvcSupport implements WebMvcConfigurer { @Autowired private LoginInterceptor loginInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { //配置攔截器 registry.addInterceptor(loginInterceptor). //對(duì)所有的資源進(jìn)行攔截,包括靜態(tài)資源 addPathPatterns("/**") .excludePathPatterns("/login.html","/js/**","/css/**","/lib/**"); }
運(yùn)行項(xiàng)目此時(shí)訪問(wèn)靜態(tài)資源就可以成功訪問(wèn):
這個(gè)問(wèn)題卡了很久很久/(ㄒoㄒ)/~~
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- springboot設(shè)置加載靜態(tài)資源的路徑(spring.resources.static-locations)
- SpringBoot中的static靜態(tài)資源訪問(wèn)、參數(shù)配置、代碼自定義訪問(wèn)規(guī)則詳解
- springboot攔截器不攔截靜態(tài)資源,只攔截controller的實(shí)現(xiàn)方法
- springboot應(yīng)用中靜態(tài)資源訪問(wèn)與接口請(qǐng)求沖突問(wèn)題解決
- SpringBoot2.x過(guò)后static下的靜態(tài)資源無(wú)法訪問(wèn)的問(wèn)題
- SpringBoot如何訪問(wèn)html和js等靜態(tài)資源配置
- springboot+thymeleaf打包成jar后找不到靜態(tài)資源的坑及解決
- SpringBoot無(wú)法訪問(wèn)/static下靜態(tài)資源的解決
- SpringBoot靜態(tài)資源及原理解析
相關(guān)文章
springboot集成redis并使用redis生成全局唯一索引ID
本文主要介紹了springboot集成redis并使用redis生成全局唯一索引ID,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Java的this關(guān)鍵字的使用與方法的重載相關(guān)知識(shí)
這篇文章主要介紹了Java的this關(guān)鍵字的使用與方法的重載相關(guān)知識(shí),是Java入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09分布式系統(tǒng)下調(diào)用鏈追蹤技術(shù)面試題
這篇文章主要為大家介紹了分布式系統(tǒng)下調(diào)用鏈追蹤技術(shù)面試問(wèn)題合集,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-03-03淺談Spring事務(wù)傳播行為實(shí)戰(zhàn)
這篇文章主要介紹了淺談Spring事務(wù)傳播行為實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09