HTTP 响应头 X-Frame-Options

简介

X-Frame-Options HTTP 响应头用来给浏览器一个指示。该指示的作用为:是否允许页面在 <frame>, </iframe> 或者 <object> 中展现。

网站可以使用此功能,来确保自己网站的内容没有被嵌套到别人的网站中去,也从而避免了点击劫持 (clickjacking) 的攻击。

重点1:当访问网页浏览器支持 X-Frame-Options 时,有效。

重点2:Content-Security-Policy (CSP) HTTP 响应头有一个名为 frame-ancestors 的指令,有相同的作用。支持CSP frame-ancestors 指令的浏览器已经废弃了 X-Frame-Options 响应头。

语法

复制代码
X-Frame-Options: DENY

或

复制代码
X-Frame-Options: SAMEORIGIN

检查 X-Frame-Options 是否已生效

以Google浏览器为例,打开网站按F12键,选择Network,找到对应的Headers,如下图所示

测试 X-Frame-Options 是否已生效

https://clickjacker.io/

Nginx 配置 X-Frame-Options

配置 Nginx 发送 X-Frame-Options 响应头,把下面这行添加到 'http', 'server' 或者 'location' 的配置中:

复制代码
add_header X-Frame-Options SAMEORIGIN always;

了解 Nginx 的 add_header 指令入口

spring boot 配置 X-Frame-Options

启用 X-Frame-Options

支持SAMEORIGIN的设置方式:

java 复制代码
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends DefaultWebSecurityConfigurer {
 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http.headers().frameOptions().sameOrigin();
 
    }
}

禁用 X-Frame-Options

java 复制代码
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends DefaultWebSecurityConfigurer {
 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http.headers().frameOptions().disable();
    }
}

参考

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/X-Frame-Options

https://blog.csdn.net/zzhongcy/article/details/124609116

https://blog.csdn.net/u014704612/article/details/115633050

相关推荐
杨凯凡18 分钟前
Spring Boot 大文件下载如何实现断点续传?HTTP Range、206 与 416 实战
spring boot·后端·http
FungLeo18 小时前
成为全栈·Vue 管理后台篇·用 fetch 建一层可控的请求内核
前端·javascript·vue.js·http·fetch·formdata·接口封装
wuyk5551 天前
Spider-第03讲-HTTP速通与读懂响应
网络·爬虫·网络协议·http
杨凯凡2 天前
Spring Framework 7 HTTP Interface 实战:声明式 RestClient、超时与测试
java·spring·http
CodeYp20192 天前
第11讲:应用层协议 HTTP
linux·http
Thanks_ks3 天前
HTTP 请求方法与 RESTful 接口设计:GET 和 POST 到底怎么用
http·api·restful·状态码·get·post·接口设计
LK_074 天前
【FastAPI】基础篇 01|开始之前:API、HTTP 与 FastAPI 到底是什么
网络协议·http·fastapi
阿星_4 天前
pydio登陆不了分析问题
网络协议·http·https
Joy T4 天前
Gzip 压缩原理与实践:HTTP 响应压缩、性能收益与工程配置
网络·网络协议·http
xixingzhe26 天前
AI 对话打字机效果:SSE 和 HTTP 的区别与适用场景
网络·网络协议·http