Spring-Security對HTTP相應(yīng)頭的安全支持方式
Spring Security支持在響應(yīng)中添加各種安全頭
默認(rèn)相應(yīng)安全頭:
Cache-Control: no-cache, no-store, max-age=0, must-revalidate Pragma: no-cache Expires: 0 X-Content-Type-Options: nosniff Strict-Transport-Security: max-age=31536000 ; includeSubDomains X-Frame-Options: DENY X-XSS-Protection: 1; mode=block
雖然這些頭文件都被認(rèn)為是最佳實踐,但應(yīng)該注意的是,并不是所有的客戶機(jī)都使用了header。
- X-Frame-Options運行同一個域名中的任何請求
- HTTP Strict Transport Security (HSTS) 將不會增加到響應(yīng)中
基于Java的配置如下:
@EnableWebSecurity public class WebSecurityConfig extends ?? ??? ?WebSecurityConfigurerAdapter { ? ?? ?@Override ?? ?protected void configure(HttpSecurity http) throws Exception { ?? ??? ?http ?? ??? ??? ?// ... ?? ??? ??? ?.headers() ?? ??? ??? ??? ?.frameOptions().sameOrigin() ?? ??? ??? ??? ?.httpStrictTransportSecurity().disable(); ?? ?} } public class WebSecurityConfig extends ?? ??? ?WebSecurityConfigurerAdapter { ?? ?@Override ?? ?protected void configure(HttpSecurity http) throws Exception { ?? ??? ?http ?? ??? ??? ?// ... ?? ??? ??? ?.headers() ?? ??? ??? ??? ?.frameOptions().sameOrigin() ?? ??? ??? ??? ?.httpStrictTransportSecurity().disable(); ?? ?} }
如果不想用默認(rèn)值可禁用,添加顯示要用的響應(yīng)安全頭
@EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { ? @Override protected void configure(HttpSecurity http) throws Exception { ?? ?http ?? ?// ... ?? ?.headers() ?? ??? ?//除非明確列出,否則不要使用任何默認(rèn)標(biāo)題。 ?? ??? ?.defaultsDisabled() ?? ??? ?.cacheControl(); } } public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { ?? ?http ?? ?// ... ?? ?.headers() ?? ??? ?//除非明確列出,否則不要使用任何默認(rèn)標(biāo)題。 ?? ??? ?.defaultsDisabled() ?? ??? ?.cacheControl(); } }
如果有必要,你可以禁用所有的HTTP安全響應(yīng)頭
@EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { ? @Override protected void configure(HttpSecurity http) throws Exception { ?? ?http ?? ?// ... ?? ?.headers().disable(); } } public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { ?? ?http ?? ?// ... ?? ?.headers().disable(); } }
在過去的Spring Security中,您需要為您的web應(yīng)用程序提供自己的緩存控制。這在當(dāng)時似乎是合理的,但是瀏覽器緩存已經(jīng)進(jìn)化到包含安全連接的緩存。
這意味著用戶可以查看經(jīng)過身份驗證的頁面,注銷,然后惡意用戶就可以使用瀏覽器歷史來查看緩存頁面。
為了幫助減輕這個Spring安全性,已經(jīng)添加了緩存控制支持,它將在您的響應(yīng)中插入以下頭部。
禁用其他安全頭,啟用緩存安全頭:
@EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { ? @Override protected void configure(HttpSecurity http) throws Exception { ?? ?http ?? ?// ... ?? ?.headers() ?? ??? ?.defaultsDisabled() ?? ??? ?.cacheControl(); } } public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { ?? ?http ?? ?// ... ?? ?.headers() ?? ??? ?.defaultsDisabled() ?? ??? ?.cacheControl(); } }
如果你真的想要緩存特定的反應(yīng),您的應(yīng)用程序可以選擇性地調(diào)用 HttpServletResponse.setHeader(String,String) 覆蓋頭部Spring Security的設(shè)置。
為了保證CSS,JavaScript之類的東西是用的并且圖像正確緩存。
@EnableWebMvc public class WebMvcConfiguration implements WebMvcConfigurer { ? ?? ?@Override ?? ?public void addResourceHandlers(ResourceHandlerRegistry registry) { ?? ??? ?registry ?? ??? ??? ?.addResourceHandler("/resources/**") ?? ??? ??? ?.addResourceLocations("/resources/") ?? ??? ??? ?.setCachePeriod(31556926); ?? ?} ? ?? ?// ... } public class WebMvcConfiguration implements WebMvcConfigurer { ?? ?@Override ?? ?public void addResourceHandlers(ResourceHandlerRegistry registry) { ?? ??? ?registry ?? ??? ??? ?.addResourceHandler("/resources/**") ?? ??? ??? ?.addResourceLocations("/resources/") ?? ??? ??? ?.setCachePeriod(31556926); ?? ?} ?? ?// ... }
內(nèi)容類型選項
歷史上的瀏覽器,包括Internet Explorer,試圖想請求的內(nèi)容類型使用 content sniffing。這就使得瀏覽器通過猜測來改善用戶體驗的內(nèi)容類型沒有指定內(nèi)容類型的資源。
例如,如果一個瀏覽器遇到一個JavaScript文件,該文件沒有指定的內(nèi)容類型,它會猜內(nèi)容類型,然后執(zhí)行它。
content sniffing的問題是,這允許惡意用戶使用polyglots(即一個文件,是作為多種內(nèi)容類型有效)來執(zhí)行XSS攻擊。
例如,某些網(wǎng)站可能會允許用戶提交一個有效的PostScript文檔到網(wǎng)站,并查看它。惡意用戶可能會創(chuàng)建一個 postscript文件,這也是一個有效的JavaScript文件 并用它執(zhí)行XSS攻擊
通過添加以下content sniffing可以禁用我們的響應(yīng)頭
X-Content-Type-Options: nosniff
HTTP Strict Transport Security (HSTS)
當(dāng)你輸入你的銀行的網(wǎng)站,你輸入mybank.example.com 或者你輸入 https://mybank.example.com 如果您省略https協(xié)議,你可能容易受到 中間人攻擊。即使網(wǎng)站執(zhí)行重定向到 https://mybank.example.com 惡意用戶能夠攔截最初的HTTP請求和操作響應(yīng)(即重定向到 https://mibank.example.com 和竊取他們的憑證)。
許多用戶忽略了https協(xié)議,這就是為什么要創(chuàng)建HTTP嚴(yán)格傳輸安全性(HSTS)的原因。一旦mybank.example.com被添加為HSTS主機(jī),瀏覽器就可以提前知道對mybank的任何請求。example.com應(yīng)該被解釋為https://mybank.example.com。這大大減少了發(fā)生中間攻擊的可能性。
將站點標(biāo)記為HSTS主機(jī)的一種方法是將主機(jī)預(yù)加載到瀏覽器中。另一種是將"Strict-Transport-Security"頭添加到響應(yīng)。例如,以下將指示瀏覽器把域作為一年的HSTS主機(jī)(一年有大約31536000秒):
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
可選includeSubDomains指令指示Spring安全子域(即secure.mybank.example.com)也應(yīng)該被視為一個 HSTS域。
只啟用HSTS在Java Configuration:
@EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { ? @Override protected void configure(HttpSecurity http) throws Exception { ?? ?http ?? ?// ... ?? ?.headers() ?? ??? ?.httpStrictTransportSecurity() ?? ??? ??? ?.includeSubdomains(true) ?? ??? ??? ?.maxAgeSeconds(31536000); } } public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { ?? ?http ?? ?// ... ?? ?.headers() ?? ??? ?.httpStrictTransportSecurity() ?? ??? ??? ?.includeSubdomains(true) ?? ??? ??? ?.maxAgeSeconds(31536000); } }
X-Frame-Options
允許給你的網(wǎng)站添加框架可能存在安全問題。例如,使用巧妙的CSS樣式用戶可能會被欺騙點擊的東西,他們不打算 (video demo)。
例如,登錄到他們的銀行用戶可能會點擊一個按鈕授予其他用戶訪問。這種攻擊被稱為 Clickjacking.
有很多方法可以減輕點擊劫持攻擊。例如,為了保護(hù)傳統(tǒng)瀏覽器不受clickjacking攻擊,您可以使用框架破壞代碼。雖然不完美,但是框架破壞代碼是您為遺留瀏覽器所能做的最好的事情。
解決點擊劫持更先進(jìn)的方法是使用 X-Frame-Options 頭:
X-Frame-Options: DENY
X-Frame-Options指示瀏覽器阻止在響應(yīng)中在框架內(nèi)呈現(xiàn)的任何站點。默認(rèn)情況下,Spring Security在iframe中禁用呈現(xiàn)。
你可以定制X-Frame-Options和 frame-options 元素。 例如,以下將指示Spring Security用 "X-Frame-Options: SAMEORIGIN" 允許iframes在同一個域:
@EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { ? @Override protected void configure(HttpSecurity http) throws Exception { ?? ?http ?? ?// ... ?? ?.headers() ?? ??? ?.frameOptions() ?? ??? ??? ?.sameOrigin(); } } public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { ?? ?http ?? ?// ... ?? ?.headers() ?? ??? ?.frameOptions() ?? ??? ??? ?.sameOrigin(); } }
X-XSS-Protection
一些瀏覽器支持過濾掉反射的XSS攻擊。這絕不是萬無一失的,但確實有助于XSS的保護(hù)。
默認(rèn)情況下,過濾通常是啟用的,因此添加header通常只會確保啟用它,并指示瀏覽器在檢測到XSS攻擊時要做什么。
例如,過濾器可能試圖以最小的入侵方式改變內(nèi)容,以使所有內(nèi)容都呈現(xiàn)出來。有時,這種類型的替換本身就會成為XSS的弱點。相反,最好是屏蔽內(nèi)容,而不是試圖修復(fù)它。為此,我們可以添加以下header:
<span style="color:#333333">X-XSS-Protection: 1; mode=block</span>
自定義java配置XSS保護(hù)
<span style="color:#333333"><em><span style="color:#808080">@EnableWebSecurity</span></em> <strong>public</strong> <strong>class</strong> WebSecurityConfig <strong>extends</strong> WebSecurityConfigurerAdapter { ? <em><span style="color:#808080">@Override</span></em> <strong>protected</strong> <strong>void</strong> configure(HttpSecurity http) <strong>throws</strong> Exception { ?? ?http ?? ?<em>// ...</em> ?? ?.headers() ?? ??? ?.xssProtection() ?? ??? ??? ?.block(false); } }</span>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java 使用ImageIO.writer從BufferedImage生成jpeg圖像遇到問題總結(jié)及解決
這篇文章主要介紹了java 使用ImageIO.writer從BufferedImage生成jpeg圖像遇到問題總結(jié)及解決的相關(guān)資料,需要的朋友可以參考下2017-03-03解析阿里一面CyclicBarrier和CountDownLatch的區(qū)別
這篇文章主要介紹了阿里一面CyclicBarrier和CountDownLatch的區(qū)別是啥,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03Tomcat Cannot assign requested address: JVM_Bind 非端口占用沖突
這篇文章主要介紹了 Tomcat Cannot assign requested address: JVM_Bind 非端口占用沖突的相關(guān)資料,需要的朋友可以參考下2017-01-01SpringBoot結(jié)果封裝和異常攔截的實現(xiàn)示例
SpringBoot 項目中,我們通常需要將結(jié)果數(shù)據(jù)封裝成特定的格式,以方便客戶端進(jìn)行處理,本文主要介紹了SpringBoot?優(yōu)雅的結(jié)果封裝和異常攔截,感興趣的可以了解一下2023-08-08Java數(shù)據(jù)結(jié)構(gòu)之稀疏矩陣定義與用法示例
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之稀疏矩陣定義與用法,結(jié)合實例形式分析了java稀疏矩陣的定義、運算、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01MyBatis JdbcType 與Oracle、MySql數(shù)據(jù)類型對應(yīng)關(guān)系說明
這篇文章主要介紹了MyBatis JdbcType 與Oracle、MySql數(shù)據(jù)類型對應(yīng)關(guān)系說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09