SpringBoot學習筆記 靜態資源處理

2022-03-11 09:13:19 字數 3861 閱讀 9240

在web開發中,靜態資源的訪問是必不可少的,如:html、、js、css 等資源的訪問。spring boot 對靜態資源訪問提供了很好的支援,基本使用預設配置就能滿足開發需求。

spring boot 對靜態資源對映提供了預設配置spring boot 預設將 /** 所有訪問對映到以下目錄:如:在resources目錄下新建 public、resources、static 三個目錄,並分別放入 a.jpg b.jpg c.jpg

瀏覽器分別訪問:

均能正常訪問相應的資源。那麼說明,spring boot 缺省會挨個從 public resources static 裡面找是否存在相應的資源,如果有則直接返回。

在實際開發中,可能需要自定義靜態資源訪問路徑,那麼可以繼承webmvcconfigureradapter來實現。

第一種方式:靜態資源配置類

package com.sam.demo.conf; import org.springframework.context.annotation.configuration; import org.springframework.web.servlet.config.annotation.resourcehandlerregistry; import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter; /** * 配置靜態資源對映 * @author sam * @since 2017/7/16 */ @configuration public class webmvcconfig extends webmvcconfigureradapter  }
重啟專案,訪問:http://localhost:8080/static/c.jpg 能正常訪問static目錄下的c.jpg資源。

spring.mvc.static-path-pattern=/static/**

重啟專案,訪問:http://localhost:8080/static/c.jpg 同樣能正常訪問static目錄下的c.jpg資源。

注意:通過spring.mvc.static-path-pattern這種方式配置,會使spring boot的預設配置失效,也就是說,/public /resources 等預設配置不能使用。

配置中配置了靜態模式為/static/,就只能通過/static/來訪問。

1.csrf(跨站請求偽造)

csrf(cross-site request forgery)跨站請求偽造,比如如果乙個站點欺騙使用者提交資料到其他伺服器的話,就會發生csrf攻擊

從spring security 3.2 開始,缺省會啟動csrf防護,實際上,除非你採取行為處理csrf防護或者將這個功能禁用,否則的話,在應用提交表單的時候你就會遇到這個問題。

spring security 通過乙個同步token的方式來實現csrf防護的功能。他會攔截狀態變換的請求並檢查csrf token。如果請求中不包含csrf token的話,或者token 不能與伺服器端的token相匹配,請求將會失敗,並丟擲csrfexception。

2.解決辦法

禁用csrf防護功能:

在web開發中,靜態資源的訪問是必不可少的,如:html、、js、css 等資源的訪問。spring boot 對靜態資源訪問提供了很好的支援,基本使用預設配置就能滿足開發需求。

spring boot 對靜態資源對映提供了預設配置spring boot 預設將 /** 所有訪問對映到以下目錄:如:在resources目錄下新建 public、resources、static 三個目錄,並分別放入 a.jpg b.jpg c.jpg

瀏覽器分別訪問:

均能正常訪問相應的資源。那麼說明,spring boot 缺省會挨個從 public resources static 裡面找是否存在相應的資源,如果有則直接返回。

在實際開發中,可能需要自定義靜態資源訪問路徑,那麼可以繼承webmvcconfigureradapter來實現。

第一種方式:靜態資源配置類

package com.sam.demo.conf; import org.springframework.context.annotation.configuration; import org.springframework.web.servlet.config.annotation.resourcehandlerregistry; import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter; /** * 配置靜態資源對映 * @author sam * @since 2017/7/16 */ @configuration public class webmvcconfig extends webmvcconfigureradapter  }
重啟專案,訪問:http://localhost:8080/static/c.jpg 能正常訪問static目錄下的c.jpg資源。

spring.mvc.static-path-pattern=/static/**

重啟專案,訪問:http://localhost:8080/static/c.jpg 同樣能正常訪問static目錄下的c.jpg資源。

注意:通過spring.mvc.static-path-pattern這種方式配置,會使spring boot的預設配置失效,也就是說,/public /resources 等預設配置不能使用。

配置中配置了靜態模式為/static/,就只能通過/static/來訪問。

1.csrf(跨站請求偽造)

csrf(cross-site request forgery)跨站請求偽造,比如如果乙個站點欺騙使用者提交資料到其他伺服器的話,就會發生csrf攻擊

從spring security 3.2 開始,缺省會啟動csrf防護,實際上,除非你採取行為處理csrf防護或者將這個功能禁用,否則的話,在應用提交表單的時候你就會遇到這個問題。

spring security 通過乙個同步token的方式來實現csrf防護的功能。他會攔截狀態變換的請求並檢查csrf token。如果請求中不包含csrf token的話,或者token 不能與伺服器端的token相匹配,請求將會失敗,並丟擲csrfexception。

2.解決辦法

禁用csrf防護功能:

spring boot 學習筆記

spring boot 學習筆記 1.有時候我們在專案啟動的時候,總是需要先啟動一些初始化的類,以前比較常見的做法是寫再static塊中,spring boot提供了乙個commandlinerunner介面,實現這個介面的類總是會被優先啟動,並優先執行commandlinerunner介面中提供的...

spring boot 學習筆記

本位參考 生成spring boot 工程,通過 spring boot 快速入門 spring boot 開發web 應用 spring boot工程結構推薦 spring boot構建restful api與單元測試 spring boot中使用swagger2構建強大的restful api文...

springboot學習筆記

restcontroller controller responsebody 控制器 value 從配置檔案中讀取元素值賦值 component 將類交給spring管理 請求引數 requestparam value id required false defaultvalue 0 請求字段 是否...