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。

 

基于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文件,该文件没有指定的内容类型,它会猜内容类型,然后执行它。

       content sniffing的问题是,这允许恶意用户使用polyglots(即一个文件,是作为多种内容类型有效)来执行XSS攻击。例如,某些网站可能会允许用户提交一个有效的PostScript文档到网站,并查看它。恶意用户可能会创建一个 postscript文件,这也是一个有效的JavaScript文件 并用它执行XSS攻击

通过添加以下content sniffing可以禁用我们的响应头

 

X-Content-Type-Options: nosniff

HTTP Strict Transport Security (HSTS)

      当你输入你的银行的网站,你输入mybank.example.com 或者你输入 https://mybank.example.com 如果您省略https协议,你可能容易受到 中间人攻击。即使网站执行重定向到 https://mybank.example.com 恶意用户能够拦截最初的HTTP请求和操作响应(即重定向到 https://mibank.example.com 和窃取他们的凭证)。

    许多用户忽略了https协议,这就是为什么要创建HTTP严格传输安全性(HSTS)的原因。一旦mybank.example.com被添加为HSTS主机,浏览器就可以提前知道对mybank的任何请求。example.com应该被解释为https://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>

微信公众号

                          

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值