亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

安全響應頭

1. 前言

上節我們討論了 Spring Security 如何防范 CSRF 攻擊,本節我們討論如何用最簡單的方式提升 Spring Security Web 項目的安全性。

Spring Security 可以通過「HTTP 安全響應頭」的方式提升安全性。

本節我們討論如何實現 HTTP 安全響應頭。

2. 安全響應頭

Spring Security 提供了一些默認 HTTP 安全性相關的響應頭。這些默認響應頭如下:

圖片描述

2.1 Cache-Control

當用戶通過了認證,訪問了敏感信息而后點擊了退出,此時若瀏覽器對頁面進行了緩存,攻擊者就可能通過點擊瀏覽器「后退」按鍵訪問到剛剛認證用戶看到的內容。

Spring Security 默認禁用瀏覽器頁面緩存,這樣可以減少敏感信息泄漏風險。

2.2 Content Type Options

瀏覽器為了增強用戶體驗,會去嗅探用戶請求的內容格式。例如當瀏覽器訪問到某個 JavaScript 文件,然而此時并沒有指定其文件類型,此時瀏覽器則會自動判斷并且執行它。

內容嗅探帶來了一些問題,比如攻擊者會利用某些可被解析成多種內容格式的文件,執行 XSS 攻擊。例如,有些網站可能允許用戶上傳和預覽 PostScript 代碼,攻擊者創建一個 PostScript 文檔,同時該文檔也是一個 JavaScript 格式文件,此時,攻擊者便可以實現 XSS 攻擊。

Spring Security 默認禁用了內容嗅探功能。

2.3 HTTP Strict Transport Security (HSTS)

通常我們訪問網站的時候是直接輸入域名,比如:mybank.example.com,此時默認情況下瀏覽器會訪問 http://mybank.example.com。HTTP 協議由于信息不加密,無法保證訪問目標的真實性,容易受到中間人攻擊。增加 Strict-Transport-Security 響應頭,可以強制將訪問協議轉換成 Https。

2.4 X-Frame-Options

允許網站被放到 iframe 對象里,這種情況下攻擊者可能通過設置 CSS,讓用戶的點擊發生在非意愿的位置。

默認情況下 Spring Security 拒絕網頁在 iframe 對象中渲染。

2.5 X-XSS-Protection

盡管大部分瀏覽器都已經內置了過濾 XSS 攻擊的支持,但也并非所有瀏覽器都默認支持。增加 XSS-Protection 頭可以確保 XSS 攻擊過濾已啟動。

3. Spring Security 配置

3.1 默認的安全頭配置

Spring Security 提供了一系列默認 Http 安全響應頭,我們可以便捷的配置它們。

例如,為 X-Frame-Options 指定值 SAMEORIGIN

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) {
        http.headers(headers -> headers.frameOptions(frameOptions -> frameOptions.sameOrigin()));
    }
}

如果不希望默認頭被自動添加,可以通過如下方式配置,例如僅添加 Cache Control 頭:

@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            // ...
            .headers(headers -> headers
                // 取消自動添加默認頭
                .defaultsDisabled()
                // 添加 CacheControl 頭
                .cacheControl(withDefaults())
            );
    }
}

或者通過以下方式禁用所有 HTTP 安全響應頭:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers(headers -> headers.disable());
    }
}

3.2 Cache Control

Spring Security 默認包含「Cache Control」頭。

如果只需要緩存指定類型的內容,我們可以通過 HttpServletResponse.setHeader(String,String) 方式指定緩存項,如 CSS、JavaScript、圖片等。

我們也可以直接禁用掉「Cache Control」頭,方式如下:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) {
        http.headers(headers -> headers.cacheControl(cache -> cache.disable()));
    }
}

3.3 Content-Type 頭

Spring Security 默認包含了 Content-Type 頭。如果需要禁用它,可以通過如下方式:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) {
        http.headers(headers -> headers.contentTypeOptions(contentTypeOptions -> contentTypeOptions.disable()));
    }
}

3.4 HSTS 頭

HTTP Strict Transport Security(HSTS)頭,在 Spring Security 中是被默認開啟的,我們可以修改它的默認狀態,如下:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .headers(headers -> headers
                .httpStrictTransportSecurity(hsts -> hsts
                    .includeSubDomains(true)
                    .preload(true)
                    .maxAgeInSeconds(31536000)
                )
            );
    }
}

3.5 X-Frame-Options 頭

默認情況下,Spring Security 通過 X-Frame-Options 頭方式禁止 iframe 層渲染,我們可以通過如下方式修改其配置:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers(headers -> headers.frameOptions(frameOptions -> frameOptions.sameOrigin()));
    }
}

3.6 X-XSS-Protection 頭

默認情況下,Spring Security 為了防止 XSS 攻擊增加了 X-XSS-Protection 頭,我們可以通過以下方式對其配置進行修改:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers(headers -> headers.xssProtection(xss -> xss.block(false)));
    }
}

3.7 自定義響應頭

Spring Security 的實現機制使得它可以輕易的添加安全響應頭。

3.7.1 靜態頭

例如配置如下自定義頭:

X-Custom-Security-Header: header-value

我們可以通過 StaticHeadersWriter 對象添加:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers(headers -> headers.addHeaderWriter(new StaticHeadersWriter("X-Custom-Security-Header","header-value")));
    }
}

3.7.2 動態添加

當Spring Security 的默認配置方式不能滿足我們添加響應頭的需求時,我們可以通過 HeadersWriter 實例動態添加。例如動態添加 X-Frame-Options 頭:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers(headers -> headers.addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsMode.SAMEORIGIN)));
    }
}

3.7.3 為指定請求添加響應頭

有時候我們只想為某個確定的請求添加響應頭,例如只針對對登錄頁面的請求,這是可以通過 DelegatingRequestMatcherHeaderWriter 實現。例如:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        RequestMatcher matcher = new AntPathRequestMatcher("/login");
        DelegatingRequestMatcherHeaderWriter headerWriter = new DelegatingRequestMatcherHeaderWriter(matcher,new XFrameOptionsHeaderWriter());
        http.headers(headers -> headers.frameOptions(frameOptions -> frameOptions.disable()).addHeaderWriter(headerWriter));
    }
}

4. 小結

本節討論了如何利用 HTTP 請求頭提升 Spring Security Web 項目的安全性,主要內容有:

圖片描述

  • Http 安全響應頭是提升 B/S 應用安全性的有效手段;
  • Spring Security 默認實現了常用的安全響應頭;
  • Spring Security 可以通過配置類,直接開啟、關閉或調整各個內置請求頭的狀態及參數;
  • Spring Security 提供了靜態設置響應頭的方法;
  • Spring Security 支持動態添加響應頭;
  • Spring Security 支持針對某個特殊請求動態設置響應頭。

下節我們討論針對 HTTP 協議,我們還能有哪些安全性提升策略。