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

相关推荐
moonless02221 天前
FastAPI框架,这一小篇就能搞懂精髓。
http·fastapi
ftpeak2 天前
从零开始使用 axum-server 构建 HTTP/HTTPS 服务
网络·http·https·rust·web·web app
weixin_456904273 天前
使用HTTPS 服务在浏览器端使用摄像头的方式解析
网络协议·http·https
拷贝码农卡卡东4 天前
pre-commit run --all-files 报错:http.client.RemoteDisconnected
网络·网络协议·http
又菜又爱玩呜呜呜~4 天前
go使用反射获取http.Request参数到结构体
开发语言·http·golang
cellurw4 天前
Linux下C语言实现HTTP+SQLite3电子元器件查询系统
linux·c语言·http
希望20174 天前
Golang | http/server & Gin框架简述
http·golang·gin
全栈技术负责人4 天前
前端网络性能优化实践:从 HTTP 请求到 HTTPS 与 HTTP/2 升级
前端·网络·http
Whisper_Yu4 天前
计算机网络(一)基础概念
计算机网络·http·https·信息与通信
emojiwoo4 天前
HTTP 状态码背后的逻辑:从请求到响应的完整流程解析(含完整流程图)
网络·网络协议·http