当前位置: 首页 > 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">&…...

衍射光学元件微结构

衍射光学元件(DOEs)是利用刻蚀微结构的衍射特性将入射光束转换为所需光分布的光学元件&#xff0c;利用结构的周期性或无周期性分别创建离散的(分束器)或连续的模式(光束整形器、扩散器)。由于这些元件的工作原理是基于光通过这些图案表面的衍射&#xff0c;因此DOE光束整形器和…...

Perplexity移动端体验崩塌实录:iOS/Android双平台1372条崩溃日志聚类分析,含Google Play App Store差评时间戳热力图

更多请点击&#xff1a; https://codechina.net 第一章&#xff1a;Perplexity用户评论汇总 Perplexity AI 作为一款以“引用驱动”为特色的问答式搜索引擎&#xff0c;自上线以来持续吸引学术研究者、开发者与技术爱好者群体。其用户评论呈现高度两极化特征&#xff1a;一方面…...

Linux 新手必会 30 个高频基础命令(零基础可直接上手)

前言对于Linux新手来说&#xff0c;无需死记硬背所有命令&#xff0c;重点掌握这30个高频基础命令&#xff0c;就能完成日常90%的操作&#xff08;目录切换、文件管理、系统查看等&#xff09;。本文按“使用场景分类”&#xff0c;每个命令标注【用法示例新手提示】&#xff0…...

告别盲调!用Keil自带的逻辑分析仪(Debug Simulator)可视化STM32引脚波形

告别盲调&#xff01;用Keil自带的逻辑分析仪(Debug Simulator)可视化STM32引脚波形 在嵌入式开发中&#xff0c;调试环节往往占据整个开发周期的40%以上时间。对于STM32开发者而言&#xff0c;传统的调试方式主要依赖LED闪烁观察或串口打印输出&#xff0c;这种方式不仅效率低…...

Linux网络数据包处理全流程:从系统调用到网卡驱动的深度解析

1. 项目概述&#xff1a;从代码到比特流的旅程如果你在Linux上写过网络程序&#xff0c;无论是用C的send()还是Python的socket.sendall()&#xff0c;你可能都曾好奇过&#xff1a;我调用完这个函数之后&#xff0c;数据到底经历了什么才变成网线上的电信号&#xff1f;反过来&…...

小程序制作平台哪个好,新手好用开发工具推荐

小程序制作平台终极对决&#xff1a;码云数智、有赞、微盟&#xff0c;谁才是你的命定之选&#xff1f;2026年的小程序赛道&#xff0c;早已不是"一招鲜吃遍天"的时代。当数字化转型成为每一个商家的必答题&#xff0c;选平台就不再是选一个工具&#xff0c;而是选一…...

从单机到联网:手把手教你用NetCA为Oracle数据库配置‘电话线’(监听程序与本地网络服务)

从单机到联网&#xff1a;手把手教你用NetCA为Oracle数据库配置‘电话线’ 想象一下&#xff0c;你刚搬进一栋新公寓&#xff0c;已经熟悉了家里的水电开关&#xff08;本地Oracle安装&#xff09;&#xff0c;但还没登记电话号码&#xff08;监听程序&#xff09;和录入邻居联…...

标签系统的底层同步拓扑:大批量客户标签异步更新的一致性方案

标签&#xff08;Tag&#xff09;是私域精细化运营的灵魂。在进行大规模广告投放、或者老客清洗时&#xff0c;企业系统经常需要同时为上万个外部客户批量追加或清空标签。 1. 标签同步的复杂性在哪里&#xff1f; 原生设计中&#xff0c;企业微信的标签是以“企业标签组&#…...

causal-learn实战指南:从算法选择到因果图解读

1. 为什么你需要causal-learn&#xff1f; 第一次接触因果发现这个概念时&#xff0c;我正被一个电商用户行为分析项目搞得焦头烂额。传统机器学习模型能准确预测用户是否会购买商品&#xff0c;但产品经理总追着我问&#xff1a;"到底哪些因素真正导致了购买行为&#xf…...

保姆级教程:用Mermaid手绘CPU流水线时空图,理解数据冒险与阻塞

可视化解析CPU流水线&#xff1a;用代码绘制时空图理解数据冒险 在计算机体系结构的学习中&#xff0c;CPU流水线技术是提升处理器性能的核心机制之一。但对于初学者而言&#xff0c;理解流水线中的数据冒险&#xff08;Data Hazard&#xff09;及其导致的阻塞现象往往充满挑战…...