1. 程式人生 > 其它 >nginx 或者tomcat 下 X-Content-Type-Options、X-Frame-Options、X-XSS-Protection安全配置

nginx 或者tomcat 下 X-Content-Type-Options、X-Frame-Options、X-XSS-Protection安全配置

引數的作用:

X-Frame-Options:響應頭表示是否允許瀏覽器載入frame等屬性,有三個配置DENY禁止任何網頁被嵌入,SAMEORIGIN只允許本網站的巢狀,ALLOW-FROM允許指定地址的巢狀;
X-XSS-Protection:表示啟用XSS過濾(禁用過濾為X-XSS-Protection: 0),mode=block表示若檢查到XSS攻擊則停止渲染頁面;
X-Content-Type-Options: 響應頭用來指定瀏覽器對未指定或錯誤指定Content-Type資源真正型別的猜測行為,nosniff表示不允許任何猜測(即關閉瀏覽器的MIME嗅探功能)。
在通常的請求響應中,瀏覽器會根據Content-Type來分辨響應的型別,但當響應型別未指定或錯誤指定時,瀏覽會嘗試啟用MIME-sniffing來猜測資源的響應型別,

這是非常危險的。例如一個.jpg的圖片檔案被惡意嵌入了可執行的js程式碼,在開啟資源型別猜測的情況下,瀏覽器將執行嵌入的js程式碼,可能會有意想不到的後果。
Strict-Transport-Security: max-age=31536000;includeSubDomains:指示瀏覽器只能通過HTTPS訪問資源,禁止HTTP的方式訪問;

一、nginx下配置Header頭設定

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "
nosniff"; add_header Strict-Transport-Security "max-age=31536000;includeSubDomains";

 二、Tomcat下配置

 <filter>
        <filter-name>httpHeaderSecurity</filter-name>
        <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
        <init-param>
            <param-name>antiClickJackingOption</param-name>
            <param-value>SAMEORIGIN</param-value>
        </init-param>
        <async
-supported>true</async-supported> </filter> <filter-mapping> <filter-name>httpHeaderSecurity</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

效果: