Spring-Security對HTTP相應頭的安全支持方式
Spring Security支持在響應中添加各種安全頭
默認相應安全頭:
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
雖然這些頭文件都被認為是最佳實踐,但應該注意的是,并不是所有的客戶機都使用了header。
- X-Frame-Options運行同一個域名中的任何請求
- HTTP Strict Transport Security (HSTS) 將不會增加到響應中
基于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(); ?? ?} }
如果不想用默認值可禁用,添加顯示要用的響應安全頭
@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(); } }
如果有必要,你可以禁用所有的HTTP安全響應頭
@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應用程序提供自己的緩存控制。這在當時似乎是合理的,但是瀏覽器緩存已經進化到包含安全連接的緩存。
這意味著用戶可以查看經過身份驗證的頁面,注銷,然后惡意用戶就可以使用瀏覽器歷史來查看緩存頁面。
為了幫助減輕這個Spring安全性,已經添加了緩存控制支持,它將在您的響應中插入以下頭部。
禁用其他安全頭,啟用緩存安全頭:
@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(); } }
如果你真的想要緩存特定的反應,您的應用程序可以選擇性地調用 HttpServletResponse.setHeader(String,String) 覆蓋頭部Spring Security的設置。
為了保證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); ?? ?} ?? ?// ... }
內容類型選項
歷史上的瀏覽器,包括Internet Explorer,試圖想請求的內容類型使用 content sniffing。這就使得瀏覽器通過猜測來改善用戶體驗的內容類型沒有指定內容類型的資源。
例如,如果一個瀏覽器遇到一個JavaScript文件,該文件沒有指定的內容類型,它會猜內容類型,然后執(zhí)行它。
content sniffing的問題是,這允許惡意用戶使用polyglots(即一個文件,是作為多種內容類型有效)來執(zhí)行XSS攻擊。
例如,某些網站可能會允許用戶提交一個有效的PostScript文檔到網站,并查看它。惡意用戶可能會創(chuàng)建一個 postscript文件,這也是一個有效的JavaScript文件 并用它執(zhí)行XSS攻擊
通過添加以下content sniffing可以禁用我們的響應頭
X-Content-Type-Options: nosniff
HTTP Strict Transport Security (HSTS)
當你輸入你的銀行的網站,你輸入mybank.example.com 或者你輸入 https://mybank.example.com 如果您省略https協議,你可能容易受到 中間人攻擊。即使網站執(zhí)行重定向到 https://mybank.example.com 惡意用戶能夠攔截最初的HTTP請求和操作響應(即重定向到 https://mibank.example.com 和竊取他們的憑證)。
許多用戶忽略了https協議,這就是為什么要創(chuàng)建HTTP嚴格傳輸安全性(HSTS)的原因。一旦mybank.example.com被添加為HSTS主機,瀏覽器就可以提前知道對mybank的任何請求。example.com應該被解釋為https://mybank.example.com。這大大減少了發(fā)生中間攻擊的可能性。
將站點標記為HSTS主機的一種方法是將主機預加載到瀏覽器中。另一種是將"Strict-Transport-Security"頭添加到響應。例如,以下將指示瀏覽器把域作為一年的HSTS主機(一年有大約31536000秒):
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
可選includeSubDomains指令指示Spring安全子域(即secure.mybank.example.com)也應該被視為一個 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
允許給你的網站添加框架可能存在安全問題。例如,使用巧妙的CSS樣式用戶可能會被欺騙點擊的東西,他們不打算 (video demo)。
例如,登錄到他們的銀行用戶可能會點擊一個按鈕授予其他用戶訪問。這種攻擊被稱為 Clickjacking.
有很多方法可以減輕點擊劫持攻擊。例如,為了保護傳統(tǒng)瀏覽器不受clickjacking攻擊,您可以使用框架破壞代碼。雖然不完美,但是框架破壞代碼是您為遺留瀏覽器所能做的最好的事情。
解決點擊劫持更先進的方法是使用 X-Frame-Options 頭:
X-Frame-Options: DENY
X-Frame-Options指示瀏覽器阻止在響應中在框架內呈現的任何站點。默認情況下,Spring Security在iframe中禁用呈現。
你可以定制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的保護。
默認情況下,過濾通常是啟用的,因此添加header通常只會確保啟用它,并指示瀏覽器在檢測到XSS攻擊時要做什么。
例如,過濾器可能試圖以最小的入侵方式改變內容,以使所有內容都呈現出來。有時,這種類型的替換本身就會成為XSS的弱點。相反,最好是屏蔽內容,而不是試圖修復它。為此,我們可以添加以下header:
<span style="color:#333333">X-XSS-Protection: 1; mode=block</span>
自定義java配置XSS保護
<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>
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
java 使用ImageIO.writer從BufferedImage生成jpeg圖像遇到問題總結及解決
這篇文章主要介紹了java 使用ImageIO.writer從BufferedImage生成jpeg圖像遇到問題總結及解決的相關資料,需要的朋友可以參考下2017-03-03解析阿里一面CyclicBarrier和CountDownLatch的區(qū)別
這篇文章主要介紹了阿里一面CyclicBarrier和CountDownLatch的區(qū)別是啥,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03Tomcat Cannot assign requested address: JVM_Bind 非端口占用沖突
這篇文章主要介紹了 Tomcat Cannot assign requested address: JVM_Bind 非端口占用沖突的相關資料,需要的朋友可以參考下2017-01-01MyBatis JdbcType 與Oracle、MySql數據類型對應關系說明
這篇文章主要介紹了MyBatis JdbcType 與Oracle、MySql數據類型對應關系說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09