欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring-Security對HTTP相應(yīng)頭的安全支持方式

 更新時間:2022年10月27日 08:55:12   作者:盲目的拾荒者  
這篇文章主要介紹了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。

基于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)文章

  • JDBC SQL語法

    JDBC SQL語法

    結(jié)構(gòu)化查詢語言(SQL)是一種標(biāo)準(zhǔn)化的語言,它允許你在數(shù)據(jù)庫上執(zhí)行操作,如創(chuàng)建項目,讀取內(nèi)容,內(nèi)容更新和刪除條目
    2014-03-03
  • java 使用ImageIO.writer從BufferedImage生成jpeg圖像遇到問題總結(jié)及解決

    java 使用ImageIO.writer從BufferedImage生成jpeg圖像遇到問題總結(jié)及解決

    這篇文章主要介紹了java 使用ImageIO.writer從BufferedImage生成jpeg圖像遇到問題總結(jié)及解決的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • 解析阿里一面CyclicBarrier和CountDownLatch的區(qū)別

    解析阿里一面CyclicBarrier和CountDownLatch的區(qū)別

    這篇文章主要介紹了阿里一面CyclicBarrier和CountDownLatch的區(qū)別是啥,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • RabbitMQ中的Channel和Exchange詳解

    RabbitMQ中的Channel和Exchange詳解

    這篇文章主要介紹了RabbitMQ中的Channel和Exchange詳解,創(chuàng)建和銷毀TCP連接很耗時,打開太多TCP連接,耗操作系統(tǒng)資源,并發(fā)量大到一定程度,系統(tǒng)的吞吐量會降低,使用一個connection多channel的方式,可以提升連接的利用率,需要的朋友可以參考下
    2023-08-08
  • Tomcat Cannot assign requested address: JVM_Bind 非端口占用沖突

    Tomcat Cannot assign requested address: JVM_Bind 非端口占用沖突

    這篇文章主要介紹了 Tomcat Cannot assign requested address: JVM_Bind 非端口占用沖突的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • Java利用字符流輕松處理文本數(shù)據(jù)

    Java利用字符流輕松處理文本數(shù)據(jù)

    在Java中,文本數(shù)據(jù)是經(jīng)常處理的一種數(shù)據(jù)類型,而字符流就是用來處理文本數(shù)據(jù)的一種流,下面就為大家介紹一下Java字符流的基本概念、常用類和方法,以及如何使用字符流來讀寫文件吧
    2023-09-09
  • SpringBoot結(jié)果封裝和異常攔截的實現(xiàn)示例

    SpringBoot結(jié)果封裝和異常攔截的實現(xiàn)示例

    SpringBoot 項目中,我們通常需要將結(jié)果數(shù)據(jù)封裝成特定的格式,以方便客戶端進(jìn)行處理,本文主要介紹了SpringBoot?優(yōu)雅的結(jié)果封裝和異常攔截,感興趣的可以了解一下
    2023-08-08
  • Java數(shù)據(jù)結(jié)構(gòu)之稀疏矩陣定義與用法示例

    Java數(shù)據(jù)結(jié)構(gòu)之稀疏矩陣定義與用法示例

    這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之稀疏矩陣定義與用法,結(jié)合實例形式分析了java稀疏矩陣的定義、運算、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • Java詳解swagger2如何配置使用

    Java詳解swagger2如何配置使用

    編寫和維護(hù)接口文檔是每個程序員的職責(zé),根據(jù)Swagger2可以快速幫助我們編寫最新的API接口文檔,再也不用擔(dān)心開會前仍忙于整理各種資料了,間接提升了團(tuán)隊開發(fā)的溝通效率
    2022-06-06
  • MyBatis JdbcType 與Oracle、MySql數(shù)據(jù)類型對應(yīng)關(guān)系說明

    MyBatis JdbcType 與Oracle、MySql數(shù)據(jù)類型對應(yīng)關(guān)系說明

    這篇文章主要介紹了MyBatis JdbcType 與Oracle、MySql數(shù)據(jù)類型對應(yīng)關(guān)系說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09

最新評論