Spring Security如何实现HTTP响应头的安全防护机制?
- 内容介绍
- 相关推荐
本文共计1052个文字,预计阅读时间需要5分钟。
目录:Spring Security 支持在响应中添加多种安全头
内容:Spring Security 支持在响应中添加以下安全头:- HTTP Strict Transport Security (HSTS)- X-Frame-Options- X-XSS-Protection- 默认响应安全头:Cache-Control: no-cache, no-store
目录
- Spring Security支持在响应中添加各种安全头
- HTTP Strict Transport Security (HSTS)
- X-Frame-Options
- X-XSS-Protection
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 mybank.example.com 如果您省略mybank.example.com 恶意用户能够拦截最初的HTTP请求和操作响应(即重定向到 mibank.example.com 和窃取他们的凭证)。
许多用户忽略了mybank.example.com。这大大减少了发生中间攻击的可能性。
将站点标记为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.
有很多方法可以减轻点击劫持攻击。例如,为了保护传统浏览器不受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>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。
本文共计1052个文字,预计阅读时间需要5分钟。
目录:Spring Security 支持在响应中添加多种安全头
内容:Spring Security 支持在响应中添加以下安全头:- HTTP Strict Transport Security (HSTS)- X-Frame-Options- X-XSS-Protection- 默认响应安全头:Cache-Control: no-cache, no-store
目录
- Spring Security支持在响应中添加各种安全头
- HTTP Strict Transport Security (HSTS)
- X-Frame-Options
- X-XSS-Protection
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 mybank.example.com 如果您省略mybank.example.com 恶意用户能够拦截最初的HTTP请求和操作响应(即重定向到 mibank.example.com 和窃取他们的凭证)。
许多用户忽略了mybank.example.com。这大大减少了发生中间攻击的可能性。
将站点标记为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.
有很多方法可以减轻点击劫持攻击。例如,为了保护传统浏览器不受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>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。

