Spring-Security对HTTP相应头的安全支持方式
作者:盲目的拾荒者 发布时间:2021-07-25 16:30:55
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 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>
来源:https://blog.csdn.net/niugang0920/article/details/79827872


猜你喜欢
- Android 如何修改APK的默认名称用Android Studio 打包App时生成的名称默认是 app-release.apk(已签名
- 操作流程假设你已经有自己的域名,因为微信公众号和微信回调都需要域名先看看官方给的文档根据官方文档,主要流程如下:(1)引导用户进入授权页面同
- 至少有K个重复字符的最长子串给你一个字符串 s 和一个整数 k ,请你找出 s 中的最长子串, 要求该子串中的每一字符出现次数都不
- 介绍本文介绍在Java程序中如何添加图片到excel表格,添加图片时可设置图片大小、位置、旋转、超链接、可选文本等,以及如何读取、删除exc
- 目录IO简介1.流Stream 2.IO流的继承结构3 File文件类3.1概述3.2创建对象3.3常用方法 3.4 练
- 本文实例讲述了C#图像边缘检测(Roberts)的方法。分享给大家供大家参考。具体如下://定义roberts算子函数private sta
- 依赖让我们先把 zip4j 依赖关系添加到我们的 pom.xml 文件中。<dependenc
- 一.模拟问题最近在公司遇到一个问题,挂号系统是做的集群,比如启动了两个相同的服务,病人挂号的时候可能会出现同号的情况,比如两个病人挂出来的号
- 单选题:(每道题目2分)1. 下列哪个声明是错误的?(B) A. int i=10;B. float f=1.1;&
- kafka作为一个使用广泛的消息队列,很多人都不会陌生,但当你在网上搜索“kafka 延迟队列”,出
- 本文实例为大家分享了android桌面悬浮窗,实现录屏时间控制显示效果的具体代码,供大家参考,具体内容如下悬浮窗效果如上图所示:很简单的一个
- 0.引言死信队列是消息队列中非常重要的概念,同时我们需要业务场景中都需要延迟发送的概念,比如12306中的30分钟后未支付订单取消。那么本期
- 线上出现了如上的 crash,第一解决反应是在 show dialog 之前做个 isFinish 和 isDestroyed 判断,当我翻
- 执行如下的jni调用:package jni;public class JNITransObject { public nativ
- 实现效果:先看下效果:需求是 滑动列表 ,其中一部分视图(粉丝数,关注数这一部分)在滑动到顶端的时候不消失,而是停留在整个界面头部。我们先分
- Android ViewGroup中的Scroller与computeScroll的有什么关系?答:没有直接的关系知道了答案,是不是意味着下
- 对上次的三个问题的个人理解: 1) 程序首先是从main函数开始执行的,假设main 函数不是 static ,就要先实例化这个类,然后调用
- 假如是在同一台机器上开发,前后端分离的工程中出现跨域问题的原因是,前端工程和后端工程运行在不同的端口上。只要协议、域名、端口有一个不同就会产
- ArrayList类List集合的实例化:List<String> l = new ArrayList<String>
- Java中字符串对象创建有两种形式,一种为字面量形式,如String str = "hello";,另一种就是使用new