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

相关推荐
爱吃小胖橘26 分钟前
Unity网络开发--超文本传输协议Http(1)
开发语言·网络·网络协议·http·c#·游戏引擎
杰瑞学AI5 小时前
我的全栈学习之旅:FastAPI (持续更新!!!)
后端·python·websocket·学习·http·restful·fastapi
Chandler241 天前
一图掌握 网络协议 核心要点
网络协议·tcp/ip·计算机网络·http
十碗饭吃不饱2 天前
WebClient工具调用HTTP接口报错远程主机断开连接
网络·网络协议·http
失散132 天前
分布式专题——33 一台新机器进行Web页面请求的历程
分布式·tcp/ip·http·路由器·交换机
Arva .3 天前
HTTP Client
网络协议·http·lua
2501_915106323 天前
CDN 可以实现 HTTPS 吗?实战要点、部署模式与真机验证流程
网络协议·http·ios·小程序·https·uni-app·iphone
千里马-horse3 天前
HTTP、WebSocket、XMPP、CoAP、MQTT、DDS 六大协议在机器人通讯场景应用
mqtt·websocket·http·机器人·xmpp·coap·fastdds
安卓开发者3 天前
鸿蒙NEXT网络通信实战:使用HTTP协议进行网络请求
网络·http·harmonyos
苜蓿草茸茸4 天前
HTTP 协议详解
网络协议·http