SpringSecurity------Security相关的HTTP响应头(三)

Security相关的HTTP响应头

有很多的HTTP响应头可以用于提高应用程序的安全级别。本节专门讨论Spring Security明确提供支持的各种HTTP响应头。如果需要,也可以使用自定义的headers。

一、Default Security Headers

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

如果默认值不能满足您的需求,您可以很容易地从这些默认值中删除、修改或添加请求头。

二、相关的部分

1、Cache Control

Spring Security的默认设置是禁用缓存,以保护用户的内容。

如果一个登录用户访问了敏感信息,然后登出,我们不希望恶意用户通过点击回退按钮去浏览敏感信息,cache control请求头需要如下设置:

Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0

为了保证应用程序在使用默认环境时的安全性,Spring Security默认添加了这些头。但是,如果应用程序提供了自己的缓存控制头,Spring Security就会退出。这允许应用程序确保像CSS和JavaScript这样的静态资源可以被缓存。

2、Content Type 选项

旧版浏览器,包括Internet Explorer,会尝试使用内容嗅探来猜测请求的内容类型。这允许浏览器通过猜测没有指定内容类型的资源上的内容类型来改善用户体验。例如,如果浏览器遇到一个没有指定内容类型的JavaScript文件,它将能够猜测内容类型,然后运行它。

内容嗅探会导致一个问题,恶意用户可以使用多种语言进行XSS攻击。例如,一些站点可能允许用户向网站提交有效的postscript文档并进行查看。恶意用户可能创建一个postscript文档,该文档也是一个有效的JavaScript文件,并使用它执行XSS攻击。例如,一些站点可能允许用户向网站提交有效的postscript文档并进行查看。恶意用户可能创建一个postscript文档,该文档也是一个有效的JavaScript文件,并使用它执行XSS攻击。

Spring Security 可以添加以下响应头来禁用内容嗅探:

X-Content-Type-Options: nosniff

3、HTTP Strict Transport Security (HSTS)

当你访问你的银行网站时,你是输入mybank.example.com还是输入https://mybank.example.com?如果忽略https协议,你可能受到中间人攻击(Man in the Middle)。即使该网站执行重定向到https://mybank.example.com,恶意用户也可以拦截初始的HTTP请求并操纵响应。

许多用户忽略了https协议,这就是创建HTTP严格传输安全(HSTS)的原因。一旦mybank.example.com被添加为HSTS主机,浏览器就可以提前知道对mybank.example.com的任何请求都应该被解释为https://mybank.example.com。这大大降低了中间人攻击(Man in the Middle)发生的可能性。

将站点标记为HSTS主机的一种方法是将主机预加载到浏览器中。另一种方法是向响应添加Strict-Transport-Security头。例如,Spring Security的默认行为是添加以下头部,它指示浏览器将域作为HSTS主机处理一年(一年大约有31536000秒):

Strict-Transport-Security: max-age=31536000 ; includeSubDomains ; preload

选项includeSubDomains指示浏览器需要将子域(比如secure.mybank.example.com)也当做HSTS域。

可选的preload指令指示浏览器,域应该作为HSTS域预加载在浏览器中。

4、HTTP Public Key Pinning (HPKP)

由于使用HPKP比较复杂,不在推荐使用。谷歌浏览器已经移除了对HPKP的支持。

5、X-Frame-Options

允许你的站点被添加到一个frame中会存在安全问题。例如,使用合适的CSS样式诱骗用户点击他们本不打算点击的东西。例如,登录到银行的用户可能会单击向其他用户授予访问权限的按钮。这种攻击被称为点击劫持(Clickjacking)。

有很多方法可以减轻点击劫持攻击。例如,要保护老式浏览器免受点击劫持攻击,可以使用帧破坏代码。虽然不是完美的,但帧破坏代码是您可以为传统浏览器所做的最好的。

解决点击劫持的一种更现代的方法是使用X-Frame-Options头。默认情况下,Spring Security禁用iframe中呈现页面的响应头如下:

X-Frame-Options: DENY

6、X-XSS-Protection

一些浏览器内置了对跨站攻击(XSS)的过滤功能。这绝不是万无一失的,但确实有助于XSS的保护。.

过滤通常在默认情况下是启用的,因此添加标头通常只是确保启用了它,并指示浏览器在检测到XSS攻击时应该做什么。例如,过滤器可能试图以侵入性最小的方式更改内容,以仍然呈现所有内容。有时,这种类型的替换本身可能成为XSS漏洞。相反,最好是屏蔽内容,而不是试图修复它。默认情况下,Spring Security使用以下头文件阻止内容:

X-XSS-Protection: 1; mode=block

7、Content Security Policy (CSP)

内容安全策略(CSP)是一种机制,web应用程序可以利用它来减轻内容注入漏洞,比如跨站点脚本(XSS)。CSP是一种声明性策略,它为web应用程序作者提供了一种工具,用于声明并最终通知客户端(user-agent) web应用程序希望从哪个源加载资源。

A web application may employ the use of CSP by including one of the following HTTP headers in the response:

  • Content-Security-Policy
  • Content-Security-Policy-Report-Only

Each of these headers are used as a mechanism(机制) to deliver(给予) a security policy(保险单) to the client. A security policy contains a set of security policy directives(指令), each responsible(可靠的) for declaring(说明) the restrictions(限制条件) for a particular(独有的) resource representation(陈述).

For example, a web application can declare(声明) that it expects(期望) to load scripts from specific, trusted sources, by including the following header in the response:

Content-Security-Policy: script-src https://trustedscripts.example.com

An attempt(试图) to load a script from another source other than what is declared in the script-src directive will be blocked by the user-agent. Additionally(此外), if the report-uri directive is declared in the security policy, then the violation(违反行为) will be reported by the user-agent to the declared URL.

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

Violation reports are standard JSON structures that can be captured either by the web application’s own API or by a publicly hosted CSP violation reporting service, such as, https://report-uri.io/.

Given the following response header, the policy declares that scripts may be loaded from one of two possible sources.

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

If the site violates this policy, by attempting to load a script from evil.com, the user-agent will send a violation report to the declared URL specified by the report-uri directive, but still allow the violating resource to load nevertheless.

8、Referrer Policy

Referrer Policy is a mechanism(机制) that web applications can leverage(利用) to manage the referrer(来源) field, which contains the last page the user was on.

Spring Security’s approach is to use Referrer Policy header, which provides different policies(策略):

Referrer-Policy: same-origin

The Referrer-Policy response header instructs(指示) the browser to let the destination(终点) knows the source where the user was previously(以前).

9、Feature Policy

Feature Policy is a mechanism that allows web developers to selectively(选择性的) enable, disable, and modify the behavior of certain APIs and web features in the browser.

Feature-Policy: geolocation 'self'

With Feature Policy, developers can opt-in to a set of “policies” for the browser to enforce on specific features used throughout your site. These policies restrict what APIs the site can access or modify the browser’s default behavior for certain features.

10、Clear Site Data

Clear Site Data is a mechanism by which any browser-side data - cookies, local storage, and the like - can be removed when an HTTP response contains this header:

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

This is a nice clean-up action to perform on logout.

11、Custom Headers

Spring Security has mechanisms to make it convenient to add the more common security headers to your application. However, it also provides hooks to enable adding custom headers.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot中配置X-Content-Security-Policy可以通过在HTTP响应中添加相应的信息来实现。根据引用\[1\]中的内容,可以使用add_header指令来添加X-Content-Security-Policy信息。具体的配置可以根据需求进行调整,以下是一个示例配置: ``` add_header X-Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self'; font-src 'self'; frame-src 'self';"; ``` 上述配置中,default-src指定了默认的资源加载策略,script-src指定了允许加载脚本的来源,style-src指定了允许加载样式的来源,img-src指定了允许加载图片的来源,font-src指定了允许加载字体的来源,frame-src指定了允许加载框架的来源。其中,'self'表示只允许从同源加载资源,'unsafe-inline'表示允许内联脚本或样式,'unsafe-eval'表示允许使用eval函数执行动态脚本。 需要注意的是,X-Content-Security-Policy信息在现代浏览器中已经被废弃,取而代之的是Content-Security-Policy信息。因此,建议使用Content-Security-Policy信息来配置内容安全策略。 另外,根据引用\[2\]中的内容,如果需要使用无限制强度加密策略,可以下载官方JCE无限制强度加密策略文件,并将local_policy.jar和US_export_policy.jar覆盖到Java安装目录下的jre\lib\security路径中。 综上所述,可以通过配置X-Content-Security-Policy信息来实现内容安全策略的配置。 #### 引用[.reference_title] - *1* [检测到目标X-Content-Type-Options X-XSS-Protection Content-Security-Policy 等响应缺失的解决办法](https://blog.csdn.net/a694704123b/article/details/126896011)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [spring cloud 中使用jasypt-spring-boot-starter 配合Nacos进行配置加密](https://blog.csdn.net/zh452647457/article/details/118559718)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [java web配置X-Content-Type-Options、X-XSS-Protection、Content-Security-Policy、X-Frame-Options安全...](https://blog.csdn.net/lxw1005192401/article/details/106187068)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豢龙先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值