当前位置: 首页 > news >正文

Spring Security漏洞防护—HTTP 安全响应头

一、默认的 Security Header

Spring Security提供了 一套默认的安全HTTP响应头,以提供安全默认值。虽然这些头信息中的每一个都被认为是最佳实践,但应该注意的是,并不是所有的客户端都使用这些头信息,所以鼓励进行额外的测试。

你可以定制特定的header。例如,假设你想使用默认值,但你希望为 X-Frame-Options 指定 SAMEORIGIN。

你可以通过以下配置做到这一点。

Customize Default Security Headers

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....headers(headers -> headers.frameOptions(frameOptions -> frameOptions.sameOrigin()));return http.build();}
}

如果你不希望添加默认值,并希望明确控制应该使用什么,你可以禁用默认值。接下来的代码列表显示了如何做到这一点。

如果你使用Spring Security的配置,下面只添加了 Cache Control。

Customize Cache Control Headers

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....headers(headers -> headers// do not use any default headers unless explicitly listed.defaultsDisabled().cacheControl(withDefaults()));return http.build();}
}

如果有必要,你可以通过以下配置禁用所有的HTTP安全响应头。

Disable All HTTP Security Headers

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....headers(headers -> headers.disable());return http.build();}
}

二、Cache Control

Spring Security默认包括 Cache Control (缓存控制)头。

然而,如果你真的想缓存特定的响应,你的应用程序可以选择性地调用 HttpServletResponse.setHeader(String,String) 来覆盖Spring Security设置的头。你可以用它来确保内容(如CSS、JavaScript和图片)被正确缓存。

当你使用Spring Web MVC时,这通常是在你的配置中完成的。你可以在Spring参考文档的 静态资源 部分找到关于如何做到这一点的细节

如果有必要,你也可以禁用Spring Security的缓存控制HTTP响应头。

Cache Control Disabled

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....headers(headers -> headers.cacheControl(cache -> cache.disable()));return http.build();}
}

三、Content Type Options

Spring Security默认包括 Content-Type 头。然而,你可以禁用它。

Content Type Options Disabled

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....headers(headers -> headers.contentTypeOptions(contentTypeOptions -> contentTypeOptions.disable()));return http.build();}
}

四、HTTP Strict Transport Security (HSTS)

默认情况下,Spring Security 提供 Strict Transport Security 头。然而,你可以明确地定制结果。下面的例子明确地提供了HSTS。

Strict Transport Security

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....headers(headers -> headers.httpStrictTransportSecurity(hsts -> hsts.includeSubDomains(true).preload(true).maxAgeInSeconds(31536000)));return http.build();}
}

五、HTTP Public Key Pinning (HPKP)

Spring Security提供了对 HTTP公钥绑定(HTTP Public Key Pinning) 的servlet支持,但 不再推荐。

你可以通过以下配置启用HPKP头。

HTTP Public Key Pinning

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....headers(headers -> headers.httpPublicKeyPinning(hpkp -> hpkp.includeSubDomains(true).reportUri("https://example.net/pkp-report").addSha256Pins("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=", "E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g=")));return http.build();}
}

六、X-Frame-Options

默认情况下,Spring Security通过使用 X-Frame-Options 指示浏览器阻止反射的XSS攻击。

例如,以下配置指定Spring Security不应再指示浏览器阻止该内容。

X-Frame-Options: SAMEORIGIN

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....headers(headers -> headers.frameOptions(frameOptions -> frameOptions.sameOrigin()));return http.build();}
}

七、X-XSS-Protection

默认情况下,Spring Security 通过使用X-XSS-Protection header指示浏览器禁用 XSS Auditor。然而,你可以改变这个默认值。例如,下面的配置指定Spring Security指示兼容的浏览器启用过滤功能,并阻止该内容。

X-XSS-Protection Customization

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....headers(headers -> headers.xssProtection(xss -> xss.headerValue(XXssProtectionHeaderWriter.HeaderValue.ENABLED_MODE_BLOCK)));return http.build();}
}

八、Content Security Policy (CSP)

Spring Security并没有默认添加 Content Security Policy(内容安全策略),因为如果不了解应用程序的上下文,就不可能知道合理的默认。web应用程序作者必须声明安全策略(或策略),以便对受保护的资源进行强制执行或监控。

考虑以下安全策略。

Content Security Policy Example

Content-Security-Policy: script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/

鉴于前面的安全策略,你可以启用CSP头。

Content Security Policy

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....headers(headers -> headers.contentSecurityPolicy(csp -> csp.policyDirectives("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/")));return http.build();}
}

要启用 CSP report-only header,请提供以下配置。

Content Security Policy Report Only

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....headers(headers -> headers.contentSecurityPolicy(csp -> csp.policyDirectives("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/").reportOnly()));return http.build();}
}

九、Referrer Policy

Spring Security 默认不添加 Referrer Policy 头。你可以通过使用配置来启用 Referer Policy 头。

Referrer Policy

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....headers(headers -> headers.referrerPolicy(referrer -> referrer.policy(ReferrerPolicy.SAME_ORIGIN)));return http.build();}
}

十、Feature Policy

Spring Security 默认不添加 Feature Policy 头。考虑一下下面的 Feature-Policy 头。

Feature-Policy Example

Feature-Policy: geolocation 'self'

你可以通过使用以下配置来启用前面的 feature policy 头

Feature-Policy

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....headers(headers -> headers.featurePolicy("geolocation 'self'"));return http.build();}
}

十一、Permissions Policy

Spring Security 默认不添加 Permissions Policy 头。考虑一下下面的 Permissions-Policy 头。

Permissions-Policy Example

Permissions-Policy: geolocation=(self)

你可以使用以下配置启用前面的 permissions policy 头。

Permissions-Policy

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....headers(headers -> headers.permissionsPolicy(permissions -> permissions.policy("geolocation=(self)")));return http.build();}
}

十二、清除网站数据(Clear Site Data)

Spring Security默认不添加 Clear-Site-Data 头。考虑一下下面的 Clear-Site-Data 头。

Clear-Site-Data Example

Clear-Site-Data: "cache", "cookies"

你可以通过以下配置在注销时发送前面的 header。

Clear-Site-Data

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....logout((logout) -> logout.addLogoutHandler(new HeaderWriterLogoutHandler(new ClearSiteDataHeaderWriter(CACHE, COOKIES))));return http.build();}
}

十三、自定义 Header

Spring Security有一些机制,可以方便地在你的应用程序中添加更常见的安全header。然而,它也提供了钩子来实现添加自定义header。

1、静态 Header

有时,你可能希望在你的应用程序中注入不支持的自定义安全header。考虑一下下面的自定义安全header。

X-Custom-Security-Header: header-value

鉴于前面的 header 信息,你可以通过使用以下配置将 header 信息添加到响应中。

StaticHeadersWriter

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....headers(headers -> headers.addHeaderWriter(new StaticHeadersWriter("X-Custom-Security-Header","header-value")));return http.build();}
}

2、HeadersWriter

当命名空间或Java配置不支持你想要的header时,你可以创建一个自定义的 HeadersWriter 实例,甚至提供一个自定义的 HeadersWriter 实现。

下一个例子使用 XFrameOptionsHeaderWriter 的一个自定义实例。如果你想明确地配置 X-Frame-Options,你可以用下面的配置来实现。

Headers Writer

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http// ....headers(headers -> headers.addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsMode.SAMEORIGIN)));return http.build();}
}

3、DelegatingRequestMatcherHeaderWriter

有时,你可能想只为某些请求写一个header。例如,也许你只想保护你的登录页面.。你可以使用 DelegatingRequestMatcherHeaderWriter 来做到这一点。

下面的配置例子使用 DelegatingRequestMatcherHeaderWriter。

DelegatingRequestMatcherHeaderWriter Java Configuration

  • Java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {@Beanpublic SecurityFilterChain filterChain(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));return http.build();}
}

相关文章:

Spring Security漏洞防护—HTTP 安全响应头

一、默认的 Security Header Spring Security提供了 一套默认的安全HTTP响应头,以提供安全默认值。虽然这些头信息中的每一个都被认为是最佳实践,但应该注意的是,并不是所有的客户端都使用这些头信息,所以鼓励进行额外的测试。 …...

Plooks大型视频在线一起看网站源码

在前段时间,因为想和异地的朋友一起看电影,但是发现有电影的地方没有一起看功能,有一起看功能的视频网站没有电影,所以就想自己做一个一起看网站,于是就有了Plooks。 Plooks是一个完整的视频网站,其中包括…...

图像处理中底层、高层特征、上下文信息理解

1.图像的语义信息: 图像的语义分为视觉层、对象层和概念层。 视觉层即通常所理解的底层,即颜色、纹理和形状等等,这些特征都被称为底层特征语义; 对象层即中间层,通常包含了属性特征等,就是某一对象在某一时刻的状态&a…...

负载均衡的算法(静态算法与动态算法)

1.静态算法 静态算法是不考虑服务器动态负载的算法,包括: (1)轮转算法:轮流将服务请求(任务)调度给不同的节点(即:服务器)。 (2)加…...

mac安装jdk

1、下载jdk(我的电脑要下载arm版,截图不对) Java Downloads | Oraclehttps://www.oracle.com/java/technologies/downloads/#jdk17-mac 2、双击安装...

WIN11+OPENCV4.8 编译及下载失败处理方法

1. 基础准备 1. 下载Opencv和Contrib库 Opencv:Releases opencv/opencv GitHub Contrib:Tags opencv/opencv_contrib GitHub 2. 安装Visual Studio 或 MinGW64 MinGW:Tags opencv/opencv_contrib GitHub 这里安装1.12.0 MinGW 。 以…...

万宾科技智能井盖传感器怎么使用?

时代在进步,科技在更新,人们身边的万事万物都在随着时代的脚步不断的前进。各种各样高科技技术在城市基础设施建设的过程中得到应用,很多智能产品不仅施工方便,而且可以向政府部门提供精准的数据,提高了相关管理人员的…...

Server Name Indication(SNI),HTTP/TLS握手过程解析

Server Name Indication(SNI)是一种TLS扩展,用于在TLS握手过程中传递服务器的域名信息。在未使用SNI之前,客户端在建立TLS连接时只能发送单个IP地址,并且服务器无法知道客户端请求的具体域名。这导致服务器需要使用默认…...

react项目实现文件预览,比如PDF、txt、word、Excel、ppt等常见文件(腾讯云cos)

使用腾讯云文档预览&#xff0c;需要开通文档预览功能&#xff0c;该功能需要收费的。 使用限制 如果需要图片预览、视频或音频可以使用获取下载链接。 页面代码 <button onClick() > {handleClick(myself/文档.xlsx)}>预览</button><div style{{ height:…...

ES SearchAPI----Query DSL语言

文章目录 Getting Startedmatch_all查询全部sort排序from\size分页_source指定字段 match匹配查询match_phrase短语匹配multi_match多字段匹配range范围查询bool复合查询must必须匹配&#xff0c;可贡献得分must_not必须不匹配&#xff0c;可贡献得分should可有可无&#xff0c…...

【STM32】HAL库——串口中断只接收到两个字符

【STM32】HAL库——串口中断只接收到两个字符 一、问题描述二、解决方法三、原因分析 一、问题描述 环境&#xff1a;STM32CubeMX(6.7.0)MDK-ARM(V5.36.0.0)STM32F103C8T6 使用XCOM发送字符串&#xff08;总共8个字符&#xff09;&#xff0c;单片机进行解析为ModBus协议失败…...

页面html结构导出为word或pdf

一、使用场景和原理 需要将当前页面(一般详情页面)或者dom容器中的内容保存/截图&#xff0c;并且导出为word或者pdf 导出word:获取dom结构直接转化为word导出 导出pdf:用canvas生成当前页面或者dom范围的快照&#xff0c;参考截图功能&#xff0c;然后将生成的canvas转为pdf内…...

Object.prototype.toString.call() 和 instanceOf 和 Array.isArray() 详解

解析: 理解 Object.prototype.toString.call(), instanceof, 和 Array.isArray() 是 JavaScript 中重要的类型检查工具。以下是一个较为详细的解释和示例代码&#xff0c;帮助你理解它们的工作原理和使用场景 Object.prototype.toString.call()&#xff1a; Object.prototyp…...

自学(黑客技术)方法——网络安全

如果你想自学网络安全&#xff0c;首先你必须了解什么是网络安全&#xff01;&#xff0c;什么是黑客&#xff01;&#xff01; 1.无论网络、Web、移动、桌面、云等哪个领域&#xff0c;都有攻与防两面性&#xff0c;例如 Web 安全技术&#xff0c;既有 Web 渗透2.也有 Web 防…...

CVE-2023-46227 Apache inlong JDBC URL反序列化漏洞

项目介绍 Apache InLong&#xff08;应龙&#xff09;是一站式、全场景的海量数据集成框架&#xff0c;同时支持数据接入、数据同步和数据订阅&#xff0c;提供自动、安全、可靠和高性能的数据传输能力&#xff0c;方便业务构建基于流式的数据分析、建模和应用。 项目地址 h…...

MySQL几种方法的数据库备份

MySQL几种方法的数据库备份_mysql备份的几种方式-CSDN博客 MySQL有几个方法来备份 最近博客一直想写点。可是不知道写什么&#xff0c;感觉自己近期的知识没有什么添加&#xff0c;今天想到了一篇能够写的博客。曾经试过依据data目录备份MySQL。可是从来没有成功过。前几天帮助…...

CI/CD:GitLab-CI 自动化集成/部署 JAVA微服务的应用合集

CI/CD&#xff1a;GitLab-CI 自动化集成/部署 JAVA微服务的应用合集 CI/CD&#xff1a;GitLab-CI 自动化集成/部署 JAVA微服务的应用合集安装DockerGitLabGitLab-Runner阿里云容器仓库 GitLab-CIJava微服务的GitLab-CI应用 CI/CD&#xff1a;GitLab-CI 自动化集成/部署 JAVA微服…...

Flask 上传文件,requests通过接口上传文件

这是一个使用 Flask 框架实现文件上传功能的示例代码。该代码定义了两个路由&#xff1a; /upload&#xff1a;处理文件上传请求。在该路由中&#xff0c;我们首先从请求中获取上传的文件&#xff0c;然后将文件保存到本地磁盘上&#xff0c;并返回一个字符串表示上传成功。 /…...

kvm webvirtcloud 如何添加直通物理机的 USB 启动U盘

第一步&#xff1a;查看USB设备ID 在物理机上输入 lsusb 命令 rootubuntu:/media/usb1# lsusb Bus 002 Device 002: ID 0781:5581 SanDisk Corp. Ultra Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub Bus 001 Device 004: ID 0424:2514 Microchip Technolo…...

html- a标签包裹img标签, 点击图片无法跳转问题记录及解决方法

html- a标签包裹img标签, 点击图片无法跳转问题记录及解决方法 首先明确我们的目标, 就是a标签包裹的内容, 都能点击后以后直接跳转 可以通过更改html结构和css解决: <div v-for"(item, index) in newsData" :key"index" class"flexcol">&…...

Vim 调用外部命令学习笔记

Vim 外部命令集成完全指南 文章目录 Vim 外部命令集成完全指南核心概念理解命令语法解析语法对比 常用外部命令详解文本排序与去重文本筛选与搜索高级 grep 搜索技巧文本替换与编辑字符处理高级文本处理编程语言处理其他实用命令 范围操作示例指定行范围处理复合命令示例 实用技…...

【根据当天日期输出明天的日期(需对闰年做判定)。】2022-5-15

缘由根据当天日期输出明天的日期(需对闰年做判定)。日期类型结构体如下&#xff1a; struct data{ int year; int month; int day;};-编程语言-CSDN问答 struct mdata{ int year; int month; int day; }mdata; int 天数(int year, int month) {switch (month){case 1: case 3:…...

JVM垃圾回收机制全解析

Java虚拟机&#xff08;JVM&#xff09;中的垃圾收集器&#xff08;Garbage Collector&#xff0c;简称GC&#xff09;是用于自动管理内存的机制。它负责识别和清除不再被程序使用的对象&#xff0c;从而释放内存空间&#xff0c;避免内存泄漏和内存溢出等问题。垃圾收集器在Ja…...

srs linux

下载编译运行 git clone https:///ossrs/srs.git ./configure --h265on make 编译完成后即可启动SRS # 启动 ./objs/srs -c conf/srs.conf # 查看日志 tail -n 30 -f ./objs/srs.log 开放端口 默认RTMP接收推流端口是1935&#xff0c;SRS管理页面端口是8080&#xff0c;可…...

ABAP设计模式之---“简单设计原则(Simple Design)”

“Simple Design”&#xff08;简单设计&#xff09;是软件开发中的一个重要理念&#xff0c;倡导以最简单的方式实现软件功能&#xff0c;以确保代码清晰易懂、易维护&#xff0c;并在项目需求变化时能够快速适应。 其核心目标是避免复杂和过度设计&#xff0c;遵循“让事情保…...

Xen Server服务器释放磁盘空间

disk.sh #!/bin/bashcd /run/sr-mount/e54f0646-ae11-0457-b64f-eba4673b824c # 全部虚拟机物理磁盘文件存储 a$(ls -l | awk {print $NF} | cut -d. -f1) # 使用中的虚拟机物理磁盘文件 b$(xe vm-disk-list --multiple | grep uuid | awk {print $NF})printf "%s\n"…...

CSS设置元素的宽度根据其内容自动调整

width: fit-content 是 CSS 中的一个属性值&#xff0c;用于设置元素的宽度根据其内容自动调整&#xff0c;确保宽度刚好容纳内容而不会超出。 效果对比 默认情况&#xff08;width: auto&#xff09;&#xff1a; 块级元素&#xff08;如 <div>&#xff09;会占满父容器…...

R语言速释制剂QBD解决方案之三

本文是《Quality by Design for ANDAs: An Example for Immediate-Release Dosage Forms》第一个处方的R语言解决方案。 第一个处方研究评估原料药粒径分布、MCC/Lactose比例、崩解剂用量对制剂CQAs的影响。 第二处方研究用于理解颗粒外加硬脂酸镁和滑石粉对片剂质量和可生产…...

无人机侦测与反制技术的进展与应用

国家电网无人机侦测与反制技术的进展与应用 引言 随着无人机&#xff08;无人驾驶飞行器&#xff0c;UAV&#xff09;技术的快速发展&#xff0c;其在商业、娱乐和军事领域的广泛应用带来了新的安全挑战。特别是对于关键基础设施如电力系统&#xff0c;无人机的“黑飞”&…...

纯 Java 项目(非 SpringBoot)集成 Mybatis-Plus 和 Mybatis-Plus-Join

纯 Java 项目&#xff08;非 SpringBoot&#xff09;集成 Mybatis-Plus 和 Mybatis-Plus-Join 1、依赖1.1、依赖版本1.2、pom.xml 2、代码2.1、SqlSession 构造器2.2、MybatisPlus代码生成器2.3、获取 config.yml 配置2.3.1、config.yml2.3.2、项目配置类 2.4、ftl 模板2.4.1、…...